Variables illustration

The current date and time is September 7th, 2010 8:34:AM

Companion Web Site

Larry Ullman has a web site devoted to PHP for the World Wide Web: Second Edition at this location.

It is intended as a supplement /complement to the book and has many helpful resources.

The Book

Variable image

PHP for the World Wide Web: Second Edition written by Larry Ullman

Chapter 2
Variables

Variables as Containers

Chapter 1 was about using PHP to do something that we can already do using HTML. Chapter 2 is where we will be finding out how PHP can make our pages dynamic and interactive. This is where variables come in. Chapter 2 is a good, basic introduction(or reintroduction) to variables.

What are Variables?

If you think of a bucket as a container for stuff, then you will understand the concept of 'variable'. A variable is assigned a value in a script in this way: $container=bucket

PHP has predefined variables and we can see what they are by running a script on the server using the variable $GLOBALS. The script will be activated by clicking $GLOBALS. This script is handy for debugging scripts because it contains the variables that exist and what their values are. The <pre></pre> tags format the print out so that it is easy to read.

Variable Syntax

All variables

Using Camel formatting has become a really good way for me for naming variables, such as $myName along with using sensible file names. Here is a simple script with variables:

It is June and the average temperature is 88. We like June!

Types of Variables

Numbers, strings and arrays are types of variables

When an array contains other arrays, it is a multidimensional array.

Here are the variables' types, in detail, with a var_dump ($GLOBALS);! It is similar to the $GLOBALS.
var_dump ($GLOBALS);
Someday I might be able to understand what all that means!

Assigning Values to Variables

Varaiables are assigned their value with the use of what is called the 'assignment operator'. We know it in another life as the equal sign. A typical variable assignment using correct syntax would look like this:
$myName= "Joanne Johnson"; and to print it to the page I would type this:
echo ""My name is $myName;" within a set of PHP tags.

Here is a simple multi-lined script:

The address is:
Park Avenue in Providence, Rhode Island.
The zip is 12345.


Quotation Marks

Double quotation marks print out the variable and single quotation marks print out the literal information enclosed within. Here is an example:

Single Quotes: name1 is $firstName $lastName
Double Quotes: name2 is Joanne Johnson

 

I had difficulty getting \n to work, but in looking around the web found that the very same thing can be accomplished with HTML in the code. So, <br /> will do it for me! However, I do understand escaping certain things like ' or".

\ is now set in my mind as it should be! I had, for whatever reason, seen it as a forward slash and it isn't! It is the backward slash.

The discussion among Bonnie, Krisse and me was very heated and we ended up learning something we won't forget! Inorder to escape something like n or r or ", it must be excepted while between double quotes.

 

On to Chapter 3!


 
<?php 
/* My Variables for the study group site */
$book "<u>PHP for the World Wide Web: Second Edition</u>";
$author "Larry Ullman";
$myName "Joanne Johnson";
$pageTitle "PHP Study Group ";

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $pageTitle ?></title>


<script src="../SpryAssets/SpryMenuBar.js" type="text/javascript"></script>
<link href="common/ullman.css" rel="stylesheet" type="text/css" />
<link href="../SpryAssets/ullmanMenu.css" rel="stylesheet" type="text/css" />
</head>

<body class="thrColHybHdr">

<div id="container">
  <div id="header">
    <h1><?php echo $pageTitle ?></h1>
    <p>Presented by <?php echo $myName ?><br />
    Started January, 2009<br />
    </p>
    <br />
  <!-- end #header --></div>
  <div id="sidebar1">
    <?php include("includes/nav.php"); ?>
    
    <h3>&nbsp;</h3>
    <p>&nbsp;</p>
    <p><img src="images/var.jpg" alt="Variables illustration" width="150" height="400" /></p>
    <p>The current date and time is <?php echo date ("F jS, Y g:i:A");?></p>
    
  <!-- end #sidebar1 --></div>
  <div id="sidebar2">
    <h3>Companion Web Site</h3>
    <p>Larry Ullman has a web site devoted to <?php echo $book ?> at this 
    <a href="http://www.dmcinsights.com/phpvqs2/" target="_blank">location</a>.</p>
    <p>It is intended as a supplement /complement to the book and has many 
    helpful resources.</p>
    <ul>
      <li><a href="http://www.dmcinsights.com/phpvqs2/errata.php">Errata</a></li>
      <li><a href="http://www.dmcinsights.com/phorum/list.php?10" target="_blank">Forum</a></li>
      <li><a href="http://blog.dmcinsights.com/" target="_blank">L. Ullman Blog</a></li>
      <li><a href="http://www.dmcinsights.com/bk_pages/faq.php?i=phpvqs2" target="_parent">FAQ</a></li>
      <li><a href="http://de.php.net/manual/en/manual.php" target="_blank">PHP Manual</a></li>
    </ul>
    <p><img src="images/book.jpg" alt="The Book" width="163" height="203" /></p>
    
    <div align="center"><img src="images/var.jpg" alt="Variable image" width="150" height="400" />
      </div>
  <!-- end #sidebar2 --></div>
  <div id="mainContent">
  <center><h3><?php echo $book ?> <span class="spanTitle" >written by</span> 
  <?php echo $author ?></h3>
    <h3>Chapter 2 <br />
      Variables</h3>
  </center>
    <p><img src="images/var_bucket.jpg" alt="Variables as Containers" width="227" 
    height="250" class="imgLeft" /></p>

    <p>Chapter 1 was about using PHP to do something that we can already 
    do using HTML. Chapter 2 is where
     we will be finding out how PHP can make our pages dynamic and interactive. 
     This is where variables come in. 
    Chapter 2 is a good, basic introduction(or reintroduction) to variables. </p>
    <h3>What are Variables?</h3>
    <p>If you think of a bucket as a container for stuff, 
    then you will understand the concept of 'variable'. 
    A variable is assigned a value in a script in this way: 
    $container=bucket</p>
    <p>PHP has predefined variables and we can see what they 
    are by running a script on the 
    server using the variable $GLOBALS. 
    The script will be activated by clicking
     <a href="globals.php" target="_blank">$GLOBALS</a>. 
    This script is handy for debugging scripts because it 
    contains the variables that exist and what their values are. 
    The &lt;pre&gt;&lt;/pre&gt; tags format the print out 
    so that it is easy to read.</p>
    <h3>Variable Syntax</h3>
    <p>All variables</p>
    <ul>
      <li> begin with $</li>
      <li>then a letter (upper or lower), or_ but never with a number</li>
      <li>can be a combination of any of the above including numbers</li>
      <li>are case sensitive</li>
      <li>are assigned value with =</li>
      <li>have no spaces</li>
    </ul>
    <p>Using Camel formatting has become a really good way
     for me for naming variables, such as 
    <em>$myName</em> along with using sensible file names.
    Here is a simple script with variables:</p>
    <div class="scriptDiv">
      <?php 
    
/* my variables */
    
$year2003;
    
$juneAvg88;
    
$june="June";
    
$message"We like June!";
    
?>
      <?php echo 
    
"It is $june and the average temperature is 
    $juneAvg. $message" 
?>
    </div>
    <h3>Types of Variables</h3>
    <p>Numbers, strings and arrays are types of variables</p>
    <ul>
      <li>Strings can be combinations of everything 
      (letters, numbers, symbols, variables)and are 
      en<img src="images/array.jpg" alt="Keys to arrays" 
      width="230" height="162" class="imgRight"/>
      closed in quotation marks</li>
      <li>Numbers are either integers or decimals and 
      are not enclosed in quotes</li>
      <li>Arrays are lists of values and are indexed by keys
       which can be numbers or strings</li>
    </ul>
    <p>When an array contains other arrays, it is a 
    multidimensional array. </p>
    <p>Here are the variables' types, in detail, with a var_dump ($GLOBALS);! 
    It is similar to the $GLOBALS.<br /> 
    <a href="var_dump.php" target="_blank">var_dump ($GLOBALS);<br />
    </a>Someday I might be able to understand what all that means!</p>
    <h3>Assigning Values to Variables</h3>
    <p>Varaiables are assigned their value with the use of what 
    is called the 'assignment operator'. 
    We know it in another life as the equal sign. 
    A typical variable assignment using correct 
    syntax would look like this:<br />
    $myName= &quot;Joanne Johnson&quot;; and to print 
    it to the page I would type this:<br />
    echo &quot;&quot;My name is $myName;&quot; within a 
    set of PHP tags.</p>
    <p>Here is a simple multi-lined script: </p>
    <div class="scriptDiv">
      <?php 
    
/* Script 2.3, variables 
    My variables */
    
$street="Park Avenue";
    
$city ="Providence";
    
$state ="Rhode Island";
    
$zip 12345;
    
    echo 
"The address is: <br />$street in $city, $state. <br />The zip is $zip."
    
?>
    </div>
    <p><br />
    </p>
    <h3>Quotation Marks</h3>
    <p>Double quotation marks print out the variable and 
    single quotation marks 
    print out the literal information enclosed within. 
    Here is an example:</p>
    <div class="scriptDiv"><?php 
    
//script 2.4, quotes
    //single or double won't matter here
    
$firstName 'Joanne';
    
$lastName "Johnson";
    
//single or double DOES matter here
    
$name1 '$firstName $lastName';
    
$name2 "$firstName $lastName";
    echo 
"<p>Single Quotes: name1 is $name1<br /> 
    Double Quotes: name2 is $name2</p>"
;
    
    
    
?>
    </div>
    <p>&nbsp;</p>
    <p>I had difficulty getting \n to work, but in looking around the web found 
    that the very same thing can be accomplished with HTML in the code. So, &lt;br /&gt; 
    will do it for me! However, I do understand escaping certain things like ' or&quot;.</p>
    <p>\ is now set in my mind as it should be! I had, for whatever reason, seen it as a 
    forward slash and it isn't! It is the 
    <a href="http://www.wsu.edu/~brians/errors/backslash.html" target="_blank">backward slash</a>.</p>
    <p>The discussion among Bonnie, Krisse and me was very heated and we ended up learning something 
    we won't forget! Inorder to escape something like n or r or &quot;, 
    it must be excepted while between double quotes.</p>
    <p>&nbsp;</p>
    <p>On to Chapter 3!</p>
  <!-- end #mainContent --></div>
<!-- This clearing element should immediately follow the 
#mainContent div in order to force the #container div to 
contain all child floats --><br class="clearfloat" />
   <div id="footer">
    <p align="center">&copy;R. Joanne Johnson 2009 </p>
    <p align="center"><a href="http://validator.w3.org/check?uri=referer" target="_blank"><img
        src="http://www.w3.org/Icons/valid-xhtml10"
        alt="Valid XHTML 1.0 Transitional" width="88" height="31" border="0" /></a></p>
  <!-- end #footer --></div>
<!-- end #container --></div>

<div id="sourceDiv">
   <pre> <?php  
   
/* shows the source code of the page */
   
show_source(basename($_SERVER['PHP_SELF'])); ?></pre>
    
  <pre><?php  
   
/* shows source for nav bar include */
  
show_source("includes/nav.php");?></pre>
</div>
<script type="text/javascript">
<!--
var MenuBar1 = new 
Spry.Widget.MenuBar("MenuBar1", {imgRight:"SpryAssets/SpryMenuBarRightHover.gif"});
//-->
</script>
</body>
</html>

<ul id="MenuBar1" class="MenuBarVertical">
          <li><a href="/ullmanBook/chapter1.php">Chapter 1</a></li>
      <li><a href="/ullmanBook/chapter2.php">Chapter 2</a></li>
  <li><a href="/ullmanBook/chapter3.php">Chapter 3</a> </li>
  <li><a href="/ullmanBook/chapter4.php">Chapter 4</a></li>
  <li><a href="/ullmanBook/chapter5.php">Chapter 5</a></li>
  <li><a href="/ullmanBook/chapter6.php">Chapter 6</a></li>
      <li><a href="/ullmanBook/chapter7.php">Chapter 7</a></li>
  <li><a href="#">Chapter 8</a></li>
      <li><a href="#">Chapter 9</a></li>
      <li><a href="#">Chapter 10</a></li>
      <li><a href="#">Chapter 11</a></li>
      <li><a href="#">Chapter 12</a></li>
      <li><a href="#">Chapter 13</a></li>
      <li><a href="/ullmanBook/index.php">Home</a></li>
       <li><a href="http://www.lzydaz.com/phpBB3/index.php" target="_blank">Hello World</a></li>
       <li><a href="http://www.lzydaz.com/studygroup/php/index.php" target="_blank">Bonnie</a></li>
       <li><a href="http://www.amaraland.com/studyGroup/index.php" target="_blank">Kc Ladybug</a></li>
       <li><a href="http://krisse.tuna.fi/phpwww/ch1.php" target="_blank">Krisse</a></li>
       
       
    </ul>