技術情報
- 2020年06月08日
- 技術情報
PHP Json
Today I will talk about how to operate Json data in PHP especially using json_encode & json_decode.
So, Lets get started. We have an array like this.
<?php
$months = array("Jan"=>"01","Feb"=>"02","March"=>"03","April"=>"04");
?>
If we want to change that array to json format, we can do like this.
<?php
$months = array("Jan"=>"01","Feb"=>"02","March"=>"03","April"=>"04");
echo json_encode($months);
?>
So What about decoding, decoding the also similar with encoding.
<?php
$json = '{"Jan":"01","Feb":"02","March":"03","April":"04"}';
//we can treat the decoded data in two ways (object & array)
//treating like an object.
$obj = json_decode($json);
echo $obj->Jan;
// treating like an array
$array = json_decode($json,true);
echo $array['Jan'];
?>
The sample concept if you want to loop through the decoded json.
<?php
$json = '{"Jan":"01","Feb":"02","March":"03","April":"04"}';
$array = json_decode($jsonobj, true);
foreach($array as $key => $value) {
echo $key . " => " . $value . "<br>";
}
<?php
$json = '{"Jan":"01","Feb":"02","March":"03","April":"04"}';
$obj = json_decode($jsonobj);
foreach($obj as $key => $value) {
echo $key . " => " . $value . "<br>";
}
By Yuuma
yuuma at 2020年06月08日 11:00:54
- 2020年06月05日
- 技術情報
[Laravel] データベースマイグレーション シーダー編
nishida at 2020年06月05日 10:00:23
- 2020年06月01日
- 技術情報
File Operations in PHP
I will talk about file operations in PHP( handling , read, write etc.). Lets take a look at reading file first.
As an example we have a file called test.txt and if we want to read the contents within the file, can simply do like this.
<?php
echo readfile("test.txt");
//this will out put the contents within the file
?>
But what if we want to handle more than reading a file, we can use fopen
<?php
//we have to open the file first along with the operation mode.
//(r,w,a,x) etc. you can see more detail as below.
// https://www.php.net/manual/en/function.fopen.php
$file = fopen("test.txt", "r") or die("Unable to open file!");
//then we read the file by specifying maximum number of bytes to read
echo fread($file,filesize("test.txt"));
// then simply close the file.
fclose($file);
?>
We can also get the first line in file using fgets
<?php
$file = fopen("test.txt", "r") or die("Unable to open file!");
echo fgets($file);
fclose($file);
?>
We can also use feof to check whether the file reaches to the end.
<?php
$file = fopen("test.txt", "r") or die("Unable to open file!");
// Output until end-of-file
while(!feof($file)) {
echo fgets($file) . "<br>";
}
fclose($file);
?>
If we want to check whether file exists or not , we can do like this
<?php
echo file_exists("test.txt");
//if exists, it will return 1
?>
By Yuuma.
yuuma at 2020年06月01日 11:00:37
- 2020年05月25日
- 技術情報
PHP Static methods
I will talk about static methods usages in php with some code samples. Lets get started.
Here is a sample, calling static function outside of the class.
<?php
class welcome {
public static function sayHi() {
echo "Welcome!";
}
}
// we can like this -> className::methodName()
welcome::sayHi();
?>
We can also call within the class like this using self::
<?php
class welcome {
public static function sayHi() {
echo "Welcome!";
}
public function msg() {
self::sayHi();
}
}
$hi = new welcome();
$hi->msg();
?>
You can also call static methods from another class like this
<?php
class welcome {
public static function sayHi() {
echo "Welcome!";
}
}
class anotherClass {
public function msg() {
welcome::sayHi();
}
}
$var = new anotherClass();
$var->msg();
?>
You can choose any ways depending upon the situations, the result will still be the same.
By Yuuma
yuuma at 2020年05月25日 11:00:23
- 2020年05月22日
- 技術情報
[Laravel] 設定ファイル「.env」の切り替えについて
nishida at 2020年05月22日 10:00:31