HTML and CSS Reference
In-Depth Information
h e output for that little script is:
Fred J . Jones donated $ 200 to charity .
As you can see by putting the dollar sign character ( $ ) into a constant, you can use it with
i nancial expressions and it won't be mistaken for a variable. By the way, you can use either
echo or print (as well as other statements) in PHP to send output to the screen.
Arrays
An array is an object that holds several values. It's like a container on a container ship where
dif erent objects are stored — dolls from China, car parts from Detroit, computers from
Japan, and corn from Iowa. h ey work just as arrays do in JavaScript, but they're coni gured a
bit dif erently. (See Chapter 12 for more on arrays.)
Arrays are named like variables except they're assigned array objects. For setting up an array,
you can use one of two basic formats. h e preferred format works like an associative array.
Instead of identifying an array element with a number, it's given a key with a value — a
key-value pair. Here's the general format for setting up an associative array:
$associate = array ( “key1” => “value1” , ”key2” => “value2” );
h e other kind of array has a numeric key. Most typically, it's set up by listing the array
elements in the following format:
334
$numeric = array ( “el0” , ”el1” , ”el2” , 3 , true);
However, it can be set up using the key=>value method as well:
$assoNum = array ( 0 => “value1” , 1 => “value2” , 1 => “value2” );
h e following little script ( array.php in this chapter's folder at www.wiley.com/go/
smashinghtml5 ), shows several dif erent combinations you can see:
<?php
$associate = array ( “key1” => “value1” , ”key2” => “value2” , ”keyEtc” => “valueEtc” );
$boxCar = array ( “tools” , ”oil drum” , ”cow” , 7 , false, “computer parts” );
$mixedBag = array ( 1 => ”first” , 2 => ”second” , ”third” => 3 , 4 => 4 );
echo $associate [ “key2” ] . “<br>” ;
echo $associate [ “keyEtc” ] . “<br>” ;
echo $boxCar [ 5 ] . “<br>” ;
echo $boxCar [ 0 ] . “<br>” ;
echo $mixedBag [ 2 ],$mixedBag [ “third” ];
?>
 
Search WWH ::




Custom Search