Using External Values

In the example we assigned a value to $firstName and $lastName using assignment statements. In most case the values for these variables would come from some external source such as a database, a form, or a file. We will look at each of these external sources later in the book.

Another way to give a value to a variable is by including it in the URL. The code shown below has been uploaded to the server.

<?php
  echo "Hello $firstName $lastName";
?>

Click this link and notice how the value of the variable is added to the URL:

hello4.php?firstName=John&lastName=Doe 

When you add values to the URL the first variable is after a question mark. Additional value follow an ampersand: &

Look at the source in hello4.html?firstName=John&lastName=Doe

Notice that in the html example you can view the source and see the PHP code, but the PHP code is not executed. In fact, you see absolutely nothing in the browser.

Experiment:

  1. Manipulate the URL in the php example to display your own name.
  2. Change the URL to lastname instead of lastName. Notice that the last name is not displayed. (Case sensitive!)
  3. Write a new script that will display first, middle, and last name.
  4. Does it matter what order you add the values to the URL?

INDEX, Help: Using the Manuals, What is a variable?, Assignment Statements, Using External Values
Next lesson: Arithmetic OperationMAIN INDEX
EXAMPLES INDEX

Copyright © Zebra0.com
All rights reserved worldwide.

 
 

Using External Values