Mathematical operators
string concatenation : with "."
$a = "Hello"; $b = "World"; Echo $a. $b;
Comparison operators
logical operators
&& and, | | Or! Non -
Other
Error suppressor
Notice Note
Warning warning
Error errors
Ternary operators
$a true ; Echo $a? " Male ":" Female ";
Sequential statements
Branch Statement : If,
$a = ten; if ($a>10) { echo "a greater than ten";} Else { echo "A is less than or equal to ten"; }
Switch case
$a= 2;Switch($a) { Case0:Echo"0000"; Break; Case1:Echo"1111"; Break; Case2:Echo"2222"; Break; default:Echo"AAAAA"; }
Looping statements
For loop
for ($i= 1; $i<10; $i+ +) {echo "Hello world<br>" ;}
While loop
$a = 0; while ($a<10) { echo "Hello world<br>"; $a+ +; }
Function
function Test () { echo "Hello World"; } Test ();
Functions with parameters
function Test ($a,$b) { echo$a+$b; } Test (10,5);
A function with a return value that accepts the return value of the method with a variable
function Test ($a,$b) { return$a+$b ; } $sum=test (10,5); Echo $sum;
Functions with variable parameters
function Test () { $arrfunc_get_args(); Var_dump ($arr); } Test (1,2,3,4,5);
Accumulation
functionTest () {$arr=Func_get_args(); $sum= 0; for($i= 0;$i<Count($arr);$i++){ $sum=$sum+$arr[$i]; } return $sum; } EchoTest (1,2,3,4,5);
Functions with default values
function Test ($a= "AA") { echo "name: {$a}"; } Test ();
Output to
Switch
function Test ($a= "AA") { echo "name: {$a}"; } Test ("BB");
Output:
a few important functions :
1. Random number function
Echo Rand ();
Generates random numbers in a range, echo rand(0,10), random numbers from 0 to 10 range
2. Get the current time (timestamp)
Echo time ();
3. Formatting timestamps
echo date("Y-m-d h:i:s",Time ()); Note: The default value is the current time
4. Encode the date time as a timestamp
echo strtotime("2017-2-3 17:12:23");
PHP (operators, statements, functions)