PHP Variables and Constants

PHP variables

PHP variables always start with a $. The following is an html
file which uses variables. Some points:

Example

<html>
<body>
   <?php
    // PHP variables always start with $.
     $jake = 4;
     $allen = 3 + $jake;
     echo $jake, " ", $allen
   ?>
   <hr>
   Some html goes here....
   <br>
   <script language="php">
     // This is another wayt to enter PHP.  And variable values survive between
     // PHP regions.
     echo '$jake has the value ', $jake, ".<br>";
   </script>
   <i>This is more HTML.</i>
   <p>
   <table>
   <?php
     // There are many pre-defined variables which describe the script's
     // environment.
     echo "<tr><td><b>My URL is:</b>:</td><td>http://",
     $_SERVER["SERVER_NAME"], ":", $_SERVER["SCRIPT_NAME"], "</td></tr>";
     echo "<tr><td><b>Your browser is</b>:</td><td>",
     $_SERVER["HTTP_USER_AGENT"], "</td></tr>";
     echo "<tr><td><b>Your IP address is</b>:</td><td>",
     $_SERVER["REMOTE_ADDR"], "</td></tr>";
   ?>
   </table></p>
   
</body>
</html>
 

Try it out now


PHP Constants

Constants can be simply defined as follows

<?php
     // Works as of PHP 5.3.0
     const CONSTANT = 'Hello World';
     
     echo CONSTANT;
?> 

 

© Lynne Grewe