PHP must known basics
PHP printing command, echo. These 3 lines print the same string “Hello World!”
echo "Hello World!"; echo "Hello "."World!"; echo "Hello ","World!";
PHP configuration information. This phpinfo() function will returen all the PHP configuratoin on your server.
echo phpinfo();
PHP interactive command console. This terminal command will bring you to the php interactive command console where you can execute php code on the fly. Ctrl-C to exit.
php -a
PHP var_dump, this function displays structured information about one or more expressions that includes its type and value. Arrays and objects are explored recursively with values indented to show structure.
$scores = array( 'Amy' => array('english'=>90,'math'=>95,'biology'=>92,'physics'=>'97'), 'Ben' => array('english'=>93,'math'=>91,'biology'=>94,'physics'=>'99'), 'Chery' => array('english'=>92,'math'=>96,'biology'=>90,'physics'=>'92') ); var_dump($scores);
PHP print_r, similary to var_dump, but it will present the information of a variable in a more human readable way.
$scores = array( 'Amy' => array('english'=>90,'math'=>95,'biology'=>92,'physics'=>'97'), 'Ben' => array('english'=>93,'math'=>91,'biology'=>94,'physics'=>'99'), 'Chery' => array('english'=>92,'math'=>96,'biology'=>90,'physics'=>'92') ); print_r($scores);
PHP get the class name and class methods of an object.
echo get_class($e); echo print_r(get_class_methods($e));
PHP dump all session variables.
var_dump($_SESSION);
PHP die() and exit(), stops the execution of the script.
die("stop here"); die(var_dump($somevar)); exit("stop here"); exit(var_dump($somevar));
PHP array.
$fruits = array('apple','banana','cherry');
PHP associative array.
$scores = array('Amy'=>95,'Ben'=>92,'Chery'=>98,'Dan'=>96);
PHP nested array.
$scores = array( 'Amy' => array('english'=>90,'math'=>95,'biology'=>92,'physics'=>'97'), 'Ben' => array('english'=>93,'math'=>91,'biology'=>94,'physics'=>'99'), 'Chery' => array('english'=>92,'math'=>96,'biology'=>90,'physics'=>'92') );
PHP foreach loop.
$fruits = array('apple','banana','cherry'); foreach($fruits as $f) { echo $f; } $scores = array( 'Amy' => array('english'=>90,'math'=>95,'biology'=>92,'physics'=>'97'), 'Ben' => array('english'=>93,'math'=>91,'biology'=>94,'physics'=>'99'), 'Chery' => array('english'=>92,'math'=>96,'biology'=>90,'physics'=>'92') ); foreach($scores as $name => $subs) { echo $name."\n<br>"; foreach($subs as $sub => $score) { echo $sub . ": ". $score." "; } echo "\n<br>"; }
PHP for loop.
$fruits = array('apple','banana','cherry'); for($i=0; $i<count($fruits); $i++) { echo $fruits[$i]." \n<br>"; }
PHP while loop.
$fruits = array('apple','banana','cherry'); $size = count($fruits); $i=0; while($i<$size) { echo $fruits[$i]." \n<br>"; $i++; }
PHP if else statements.
$num = rand(0,10); // generate a random number from 0 to 10 inclusive if($num==0) echo $num . " is zero."; else if($num%2 ==0) echo $num . " is an even number."; else echo $num . " is an odd number.";
PHP class definition.
class Person { public $name; public $age; function __construct($name, $age) { $this->name = $name; $this->age = $age; } public function speak() { echo "Hello, my name is ".$this->name.", I am ".$this->age." years old."; } } $p = new Person("Amy",18); $p->speak();
PHP inheritance.
class Employee extends Person { public $career; function __construct($name, $age, $career) { parent::__construct($name, $age); $this->career = $career; } //override the parent speak. public function speak() { parent::speak(); echo "My career is " . $this->career; } } $e = new Employee('Ben',22,'Programmer'); $e->speak();
Search within Codexpedia
Search the entire web