Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

PHP Arithmetic Operators Example

<html>
   
   <head>
      <title>Arithmetical Operators</title>
   </head>
   
   <body>
   
      <?php
         $a = 42;
         $b = 20;
         
         $c = $a + $b;
         echo "Addtion Operation Result: $c <br/>";
         
         $c = $a - $b;
         echo "Substraction Operation Result: $c <br/>";
         
         $c = $a * $b;
         echo "Multiplication Operation Result: $c <br/>";
         
         $c = $a / $b;
         echo "Division Operation Result: $c <br/>";
         
         $c = $a % $b;
         echo "Modulus Operation Result: $c <br/>";
         
         $c = $a++; 
         echo "Increment Operation Result: $c <br/>";
         
         $c = $a--; 
         echo "Decrement Operation Result: $c <br/>";
      ?>
   
   </body>
</html>

Static Variables in PHP

<?php
   function keep_track() {
      STATIC $count = 0;
      $count++;
      print $count;
      print "<br />";
   }
   
   keep_track();
   keep_track();
   keep_track();
?>

Global Variables in PHP

<?php
   $somevar = 15;
   
   function addit() {
      GLOBAL $somevar;
      $somevar++;
      
      print "Somevar is $somevar";
   }
   
   addit();
?>

Function Parameters in PHP

<?php
   // multiply a value by 10 and return it to the caller
   function multiply ($value) {
      $value = $value * 10;
      return $value;
   }
   
   $retval = multiply (10);
   Print "Return value is $retval\n";
?>

Local Variables in PHP

<?php
   $x = 4;
   
   function assignx () { 
      $x = 0;
      print "\$x inside function is $x. <br />";
   }
   
   assignx();
   print "\$x outside of function is $x. <br />";
?>

Here Document

<?php
   $channel =<<<_XML_
   
   <channel>
      <title>What's For Dinner</title>
      <link>https://menu.example.com/ </link>
      <description>Choose what to eat tonight.</description>
   </channel>
_XML_;
   
   echo <<<END
   This uses the "here document" syntax to output multiple lines with variable 
   interpolation. Note that the here document terminator must appear on a line with 
   just a semicolon. no extra whitespace!
   

END;
   
   print $channel;
?>

PHP String Variable

<?php
   $variable = "name";
   $literally = 'My $variable will not print!';
   
   print($literally);
   print "<br>";
   
   $literally = "My $variable will print!";
   print($literally);
?>

PHP Doubles

<?php
   $many = 2.2888800;
   $many_2 = 2.2111200;
   $few = $many + $many_2;
   
   print("$many + $many_2 = $few <br>");
?>

Running PHP Script

<?php
   echo "Hello PHP!!!!!";
?>

PHP is case sensitive

<html>
   <body>
      
      <?php
         $capital = 67;
         print("Variable capital is $capital<br>");
         print("Variable CaPiTaL is $CaPiTaL<br>");
      ?>
      
   </body>
</html>

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.