PHP   17

Variables and data types in PHP

Guest on 15th May 2022 04:23:21 AM

  1. <!DOCTYPE html>
  2. <html>
  3.  <head>
  4.   <title>Variables and data types in PHP</title>
  5.  </head>
  6.  <body>
  7. <?php
  8.   // All PHP variables beging with the $
  9.   // and variable names can begin with an underscore after $
  10.   /* PHP is dynamically typed, i.e., no type declarations
  11.      Because it's an interpreted language,
  12.      an interpreter recognizes a variable's type at run time.
  13.      A variable's type may change throughout program execution */
  14.  
  15.   $x = 5;         // integer
  16.   echo $x . "<br />";
  17.  
  18.   $y = 3.2;       // float
  19.   echo $y . "<br />";
  20.  
  21.   $x = $x + $y;   // float
  22.   echo $x . "<br />";     // to get variable's type, use a gettype(var) function
  23.   echo gettype($x) . "<br />";
  24.  
  25.   $x = $x . " dollars";   // string, concatenation with a dot (.)  
  26.   echo "\$x is $x and is of type " , gettype($x), "<br />";      // use a comma separate
  27.   echo "\$x is $x and is of type " . gettype($x) . "<br />";     // use a string concatenation
  28.  
  29.  
  30.   $mystring = "hello";
  31.   echo "<br />To instruct the interpreter to <b>not</b> execute \$mystring as a variable
  32.                 but simply treat it as a string and display it on the screen,
  33.                 use an escape character \\, for example, \\\$mystring <br />";
  34.   echo "\$mystring = " . $mystring . "<br />";
  35.   echo "Use an escape character \\, \"$mystring\"" . "<br />";  
  36.   echo "\$mystring[1] = ", $mystring[1], "<br />";
  37.  
  38.   echo "<br />";
  39.  
  40.   $x = 1234;
  41.   echo var_dump($x), "<br />";    // var_dump() return both type and content of a variable
  42.  
  43.   if (is_int($x))
  44.         echo $x . " is an int" . "<br />";
  45.   else
  46.         echo $x . " is not an int <br />";
  47.  
  48.   if (is_float($x))
  49.         echo $x . " is a float" . "<br />";
  50.   else
  51.     echo $x . " is not a float <br />";
  52.                  
  53.   if (is_numeric($x))
  54.         echo $x . " is a numeric" . "<br />";
  55.   else
  56.         echo $x . " is not a numeric <br />";
  57.  
  58.   if (is_string($x))
  59.         echo $x . " is a string" . "<br />";
  60.   else
  61.         echo $x . " is not a string <br />";
  62.   echo "<br />";
  63.        
  64.   $t = "98765";
  65.   echo "\$t = \"$t\"" . "<br />";
  66.   if (is_int($t))
  67.         echo $t . " is an int";
  68.   else
  69.         echo $t . " is not an int";
  70.   echo "<br />";
  71.  
  72.   echo "cast : \$t = (int)\$t <br />";
  73.   $t = (int)$t;
  74.   if (is_int($t))
  75.         echo $t . " is an int";
  76.   else
  77.         echo $t . " is not an int";
  78.   echo "<br />";
  79.  
  80.   echo "cast : \$t = (float)\$t <br />";
  81.   $t = (float)$t;
  82.   if (is_float($t))
  83.         echo $t . " is a float";
  84.   else
  85.         echo $t . " is not a float";
  86.   echo "<br />";
  87.  
  88.   // Other ways to display things on the screen
  89.   //
  90.   // The print function is used to create simple unformatted output.
  91.   // (similar to the echo function)
  92.   //
  93.   // PHP borrows the printf function from C.
  94.   //
  95.   // The print doesn't require parentheses
  96.   // whereas printf requires
  97.    
  98.   $str = "PHP is fun";  
  99.   $sub = substr($str, 7, 3);   // string operations
  100.   print $str{2};       // display a character in string at the position 2 (index starts at 0)
  101.   print "<br />";
  102.   printf($sub);
  103.   printf("<br />");
  104.   printf(strtolower($str) . "<br />");  
  105.   printf(strtoupper($str) . "<br />");
  106.  
  107.   $day = "Tuesday";
  108.   $high = 57;  
  109.   printf("The high on %7s was %3d <br />", $day, $high);   // syntax similar to C
  110.   printf("%5.2f", $high);
  111.   // %7s -- a character string field of 7 characters
  112.   // %3d -- an integer field of 3 digits
  113.   // %5.2f -- a float or double field in xx.xx format
  114.  
  115. ?>
  116. </body>
  117. </html>

Raw Paste


Login or Register to edit or fork this paste. It's free.