Posts

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. 

Numbers in PHP

 PHP Integers Check if variable type is Integer <?php $x =  5985 ; var_dump(is_int($x)); $x =  59.85 ; var_dump(is_int($x)); ?> PHP Floats Check if variable type is Floats <?php $x =  10.365 ; var_dump(is_float($x)); ?> PHP Infinity Check if value in infinite or finite <?php $x =  1.9e411 ; var_dump($x); ?> PHP NAN Check if invalid calculation it return NAN <?php $x = acos( 8 ); var_dump($x); ?> PHP Numerical String <?php $x =  5985 ; var_dump(is_numeric($x)); $x =  "5985" ; var_dump(is_numeric($x)); $x =  "59.85"  +  100 ; var_dump(is_numeric($x)); $x =  "Hello" ; var_dump(is_numeric($x)); ?> Thanks for Reading. I hope it helps. Want to donate. Donate me Here

String In PHP

 There are following string functions in PHP: strlen() - in return the length of string. <?php echo  strlen( "Learn PHP by RK" );  // outputs 15 ?> str_word_count() - Count Words in a String <?php echo  str_word_count( "Learn PHP by RK" );  // outputs 4 ?> strrev() - Reverse a String <?php echo  strrev( "Hello world!" );  // outputs !dlrow olleH ?> strpos() - Search For a Text Within a String <?php echo  strpos( "Hello world!" ,  "world" );  // outputs 6 ?> str_replace() - Replace Text Within a String <?php echo  str_replace( "world" ,  "Dolly" ,  "Hello world!" );  // outputs Hello Dolly! ?> Thanks for reading. I hope It helps. Donate Me

Data Types in PHP

  PHP supports the following data types: String Integer Float (floating point numbers - also called double) Boolean Array Object NULL Resource There are some examples of data types in PHP, you can use it understand the concept: String <?php $x =  "Learn PHP By RK" ; $y =  'Learn Wordpress By RK' ; echo  $x; echo   "<br>" ; echo  $y; ?> Integer <?php $x =  9685 ; var_dump($x); ?> FLOAT <?php $x =  18.635 ; var_dump($x); ?> Boolean $x = true; $y = false; Array <?php $cars =  array ( "Iron Man" , "Hulk" , "Thor" ); var_dump($cars); ?> Object In the following example car in Class having properties color and model. <?php class  Car {    public  $color;    public  $model;    public   function  __construct($color, $model) {      $this->color = $color;     $this->model = $model;    }    public   function  message() {      return   "My car is a "  . $this->color .  " "  . $thi