This topic we'll show the basic and the language essentials to start and app with PHP. PHP is intented to provide simplicity in development process.
/*
<?php ?> - Standard Tags, enabled by default
<?= ?> - Echo Tags, Default PHP 5.4+
<? ?> - Short Tags, PHP Ini option - Default off
<% %> - ASP Tags, PHP Ini option - Default off, Removed on PHP 7.0
*/Scalar Types:
integer - a signed numeric value
boolean - a value that can be true or false
float - signed floating-point range numeric value
string - a collection of binary data
Composite Types:
array - container of mixed data, from scalar types to objects or arrays inside arrays
object - container of code and data. they are the basis of object oriented programming
Special Types:
resource - external resources that are not natively of PHP like file handling, image manipulation.
null - indicate variable that has no value and has set to the special value NULL or if the value hasn't been set on the variable.
//Store a value
$carOneValue = 1;
/* Reference carOneValue variable to the new $carOneValueReference.*/
$carOneValueReference = &$carOneValueReference;
//Change value to the two variables
$carOneValueReference = 100.80$myVar = 'someValue';
$$myVar = 'anotherValue';
echo $someValue; //prints anotherValue$carOneValue = 1;
function sumCarValue(&$carValue, $plusValue = 0) {
return $carValue = $carValue + $plusValue;
}
sumCarValue($carOneValue, 1000);
var_dump($carOneValue);if (isset($someVar)) {
//she exists
} else {
//She didn't exists
}
//or check directly, i recommend avoid using 'else' as possibe in PHP
if (!isset($someVar)) {
//she didn't exist
}$a = 1;
$a = (string) $a;
$b = 10.8;
$b = (int) $b;
$c = '10.82';
$c = (float) $c;There are some functions to cast types if you prefer, see in the table below:
| Tag | Description |
|---|---|
| intval() | Cast the given variable to an integer |
| floatval() | Cast the given variable to a float |
| strval() | Cast the given variable to a strin |
| boolval() | Cast the given variable to a boolean |
| settype() | Cast the given variable to a given type |
// Single line comments
# Single line comments
/*
Multi-line
Comments
*/
/**
* API Documentation Example
*
* @param string $bar
*/
function foo($bar) { }- You cannot have any whitespace between <? and php
- You cannot break apart keywords (e.g.: whi le, fo r, and funct ion)
- You cannot break apart variable names and function names (e.g.:
- $var name and function foo bar())
- Heredoc and Nowdoc closing identifiers must not be preceded by
- anything, including whitespace.
//If statements
if (expression) {
//Some code here
} else {
//Some other expressions here
}
//Function statements
function car() {
//Some code here
}
//Function call
car();Check on the php.net manual, because some constructors can accept parameters like a function
exit;
die; //a nickname for exit
echo;
return;
print;empty();
eval();
include e include_once //generate warning if file not found
require e require_once //generate fatal error if file not found
isset()
unset()
list()__LINE__;
__FILE__;
__DIR__;
__FUNCTION__;
__CLASS__;
__TRAIT__;
__METHOD__;
__NAMESPACE__;Bear in mind to check operator precende in PHP Manual. Assignment operator:
$a = 'by value';
$b = &$a;
echo $a;
echo PHP_EOL; //php end of line constant
echo $b;
echo PHP_EOL;
$b = 'Changed value in the reference variable';
echo $a;
echo PHP_EOL;
echo $b;Arithmetic operator:
$plus = 2 + 2;
$minus = 2 - 2;
$multiplication = 2 * 2;
$division = 2 / 2;
$mod = 2 % 2;Referencing values:
$a = 1;
$b = &$a;Bitwise operator: With sprintf with leading zeros on the left and decbin, we can check the binary value of our two to check bits that are active or not and sum the result accoding to which operator we're using
// NOT - OPERATOR
$a = ~1; //0
// AND - Share the same bit active
$a = 8;
$b = 10;
echo sprintf('%08s', decbin($a));
echo '<br>';
echo sprintf('%08s', decbin($b));
echo '<br>';
echo $a & $b; //8
//OR - OPERATOR
$a = 9;
$b = 10;
echo sprintf('%08s', decbin($a));
echo '<br>';
echo sprintf('%08s', decbin($b));
echo '<br>';
echo $a | $b; //11
//XOR - OPERATOR
$a = 9;
$b = 10;
echo sprintf('%08s', decbin($a));
echo '<br>';
echo sprintf('%08s', decbin($b));
echo '<br>';
echo $a ^ $b; //3Bitwise multiplication & division:
//Multiplication by the power of 2
$x = 2;
echo $x << 1; // Outputs 4
echo $x << 2; // Outputs 8
//Division by the power of 2
$x = 4;
echo $x >> 1; // Outputs 2
echo PHP_EOL;
echo $x >> 2; // Outputs 1Equivalence:
'a' == 'b' //false
'1' == '1' //trueIdentity:
'a' === 'b' //false
1 === '1' //false
1 === 1 //trueNot equivalent:
'a' != 'b' //true
'1' != '1' //falseNot identical:
'a' !== 'b' //true
'a' !== 'a' //false
1 !== '1' //true
1 !== 1 //falseLess than and less & equals to:
15 <= 15 //true
15 < 15 //false
15 < 16 //trueGreater than and greater & equals:
15 >= 15 //true
15 > 15 //false
16 > 15 //trueSpaceship operator:
1 <=> 1 //0
1 <=> 2 //-1
2 <=> 1 //1Null Coalescing operator
$a = ['a' => 1, 'b' => 3];
$two = $a['2'] ?? '2';And:
$a = 1;
$b = 2;
$a === 1 && $b === 2 //true if both are true
$a === 1 and $b === 3 //false if both are true, the change here is operator precedenceNot:
var_dump(!a) //false
//Or
$a === 1 || $b === 3 //true if any of expressions evaluate to true
$a === 2 or $b === 2 //true if any of expressions evaluate to true, the change here is operator precedenceXor:
$a === 1 xor $b === 3 //true if only one of expressions evaluate to true
$a === 1 xor $b === 2 //false if only one of expressions evaluate to true, the change here is operatorBackticks:
$output = `ls -al`;
echo "<pre>$output</pre>";x = @fopen("/tmp/foo"); //Ignore default error messagesConcatenation:
$a = 'Studing';
$b = 'ZCE';
echo $a .' '. $b;Concatenating assignment:
$a = 'Studing';
$a .= ' ZCE';
echo $a;Union:
$a = ['one' => 1, 'two' => 2];
$b = ['one' => 2, 'two' => 4, 'three' => 3];
print_r($a + $b);
print_r($a += $b);Equality:
$a = ['one' => '1', 'two' => 2];
$b = ['one' => 1, 'two' => 2];
print_($a == $b); //true
print_($a === $b); //falseInequality:
$a = ['one' => '1', 'two' => 2];
$b = ['one' => 1, 'two' => 2];
print_($a != $b); //false
print_($a <> $b); //falseNon-indentity:
$a = ['one' => '1', 'two' => 2];
$b = ['one' => 1, 'two' => 2];
print_($a !== $b); //trueType operators:
class A {}
class B extends A {}
class C {}
$b = new B;
$c = new C;
var_dump($b instanceof B); //Instance of A either B
var_dump($c instanceof A); //falseIf then else:
if (exp1) {
//some code
} elseif (exp2) {
//some code
} else {
//some code
}Nested if then else:
if ($omething) {
//some code
if ($somethingElse) {
//some code
} else {
//some code
}
} else {
//some code
}Ternary:
$queryParamsArray = (isset($_GET['q'])) ? explode('&', $_GET['q']) : false;
print_r($queryParamsArray);Switch:
$a = ['one' => '1', 'two' => '2'];
switch ($a) {
case 'one':
# code...
break;
case 'two':
# code...
break;
default:
# code...
break;
}Foreach:
$a = ['one' => '1', 'two' => '2'];
//Lopp keys and values
foreach ($variable as $key => $value) {
if ($key === 'one') {
echo 'Exited on one';
break;
}
}
//Loop only values
foreach ($variable as $value) {
# code...
}For:
for ($i=0; $i < 10; $i++) {
if ($i === 6) {
continue;
}
echo $i; //012345789
}While:
$i = 1;
while ($i <= 10) {
echo $i++;
}This is better check files in Namespaces section