In this article we will teach you some PHP Basics like Data types, Mathematical operators, Comparison operators, Comments, Conditional Statements, Loops, Global functions and Functions.
PHP Basics
Data types
integers (2), floats (2.33), booleans (true/false), strings (‘hello’)
Mathematical operators
+ addition, as in 5 + 2
– subtraction, as in 5 – 1
* multiplication, as in 5 * 2
/ division, as in 6 / 2
% modulus or the remainder of a division operation, as in 4 % 2
Comparison operators
== returns true if the value in the left side of the operator matches the value in the right side; 5 == 5 would be true, 5 == ‘5’ would also result to true while 4 == 5 would return the boolean false
=== returns true if the value in the left side of the operator is the same as the value of the right side of the operator and if they are both of the same data type. Therefore, 5 === 5 would return true, 5 === ‘5’ would return false, as well as 4 === 5.
> this operator would return true if the value in the left side is greater than the value in the right side (greater than), 4 > 5 would return false while 5 > 4 would return true. Strings are converted to numbers and then compared.
< the less than operator; opposite way of working of greater than.
Comments
You can use // to comment out the entire line after that sequence of characters. You can also use
/*
Multi-line comments
Here
*/ to comment out entire lines.
If statement
You can use the if statement to execute a piece of code only if a particular condition is met; as in
if (5 > 3) { // some application logic }
The if conditional would check if the statement in it returns true and execute the code inside it only in such a case.
After an if statement, you can use else to execute code if the if statement is falsy:
If (5>3) { // do something if 5 > 3 } else { // do something only if 5 is not greater than 3 }
Loops
You can use foreach ($array as $property) to loop over each array item, like this:
foreach ($names as $name) { echo $name . " was here"; }
You can use the for loop to iterate a particular number of times, like this:
for ( $i = 0; $i < 10; $i++) { echo $i; }
We initialize the $i variable in the loop to equal zero, add one to it on each iteration and repeat this process until $i is less than 10.
Global functions
PHP has numerous built-in functions that you can use out of the box. They can help you achieve certain tasks without starting from scratch. Functions are just like helpers to which you pass certain values and get a specific result. One such function is mt_rand. You use mt_rand to generate a random number and use it in the following way:
mt_rand(0,999) // this will generate a random number between 0 and 999.
There are also a variety of functions to help you with string operations such as substr, substr_count, strpos, strlen with arrays such as count, array_shift, array_pop and so on. You can view all built-in functions in PHP’s official documentation, located at: http://php.net/manual/en/
Functions
You can create your own functions which will help you not repeating code when you have to execute a piece of logic multiple times. Here is a simple function which adds two numbers and returns them, then they are displayed.
function add($n1,$n2) { return $n1 + $n2; } echo add(1,2);
Leave a Reply