Posts

Showing posts from September, 2022

if...else...elseif Statements in PHP

Image
If & else statement also knows as conditional statements. In this blog we will learn how to use conditional statements in PHP. We can use conditional statements in PHP by following ways: if statement - executes some code if one condition is true if...else statement - executes some code if a condition is true and another code if that condition is false if...elseif...else statement - executes different codes for more than two conditions switch statement - selects one of many blocks of code to be executed if statement in PHP: Syntax: if (condition) {   code to be executed if condition is true; } Example: Check Currently AM <?php $t = date("H"); if ($t < "12") {   echo "It is currently AM"; } ?> if...else statement in PHP: Syntax: if (condition) {   code to be executed if condition is true; } else {   code to be executed if condition is false; } Example: Check Currently AM or PM   <?php $t = date("H"); if ($t < "20") {  

PHP Operators

 There are following types of operator in PHP: Arithmetic operators Assignment operators Comparison operators Increment/Decrement operators Logical operators String operators Array operators Conditional assignment operators Arithmetic operators: + Addition $x + $y Sum of $x and $y - Subtraction $x - $y Difference of $x and $y * Multiplication $x * $y Product of $x and $y / Division $x / $y Quotient of $x and $y % Modulus $x % $y Remainder of $x divided by $y ** Exponentiation $x ** $y Result of raising $x to the $y'th power Assignment operators: x = y x = y The left operand gets set to the value of the expression on the right x += y x = x + y Addition x -= y x = x - y Subtraction x *= y x = x * y Multiplication x /= y x = x / y Division x %= y x = x % y Modulus Comparison operators: == Equal $x == $y Returns true if $x is equal to $y === Identical $x === $y Returns true if $x is equal to

PHP Constants

 A PHP cosntant is an identifier  which value cannot be changed during the script. A valid constant name start with letter or underscore(no other special character or number). Syntax: define(name, value, case-insensitive) Example: <?php define("GREETING", "Welcome to W3Schools.com!"); echo GREETING; ?> Array constant in PHP: <?php define("cars", [   "Alfa Romeo",   "BMW",   "Toyota" ]); echo cars[0]; ?> Constants are global in PHP you can use it anywhere see below example: <?php define("GREETING", "Welcome to W3Schools.com!"); function myTest() {   echo GREETING; }   myTest(); ?>

PHP Math Functions

 Php has some predefined math functions to calculation mathematics problems. Some of them are listed below: 1) Php pi() function: <?php echo(pi()); // returns 3.1415926535898 ?> 2) Php min() & max() function:  <?php echo(min(0, 150, 30, 20, -8, -200));  // returns -200 echo(max(0, 150, 30, 20, -8, -200));  // returns 150 ?> 3) Php abs() function:  <?php echo(abs(-6.7));  // returns 6.7 ?> 4) Php sqrt() function:  <?php echo(sqrt(64));  // returns 8 ?> 5) Php round() function:  <?php echo(round(0.60));  // returns 1 echo(round(0.49));  // returns 0 ?> 6) Php rand() function:  <?php echo(rand()); ?> For complete math functions you can visit w3schools.com website.