DOWNLOAD THE CODE FOR THIS BOOK FROM
- STATIC VARIABLE VS DYNAMIC WEBSITES
- PHP: THE LANGUAGE AND THE INTERPRETER
- PERFORMING A TASK USING DIFFERENT DATA
- WHAT IS A PHP PAGE?
- WHAT IS MYSQL?
- HISTORY OF PHP
- HISTORY OF MYSQL
- WHAT THIS BOOK COVERS
- A. BASIC PROGRAMMING INSTRUCTIONS
- B. DYNAMIC WEB PAGES
- DATABASE DRIVEN WEBSITES
- EXTENDING THE SAMPLE APPLICATION
- INSTALLING SOFTWARE AND FILES
- DOWNLOADING THE SAMPLE CODE
- HOW PHP PAGES MIX HTML & PHP CODE
Variables store data that can change (or vary) each time a PHP page is requested. They use a name to represent a value that can change.
<?php
$name = 'Ivy';
$price = 5;
?>
<!DOCTYPE html>
<html>
<head>
<title>Variables</title>
<link rel="stylesheet" href="css/styles.css">
</head>
<body>
<h1>The Candy Store</h1>
<h2>Welcome <?php echo $name; ?></h2>
<p>The cost of your candy is
$<?php echo $price; ?> per pack.</p>
</body>
</html>
<?php
$name = 'Guest';
$name = 'Ivy';
$price = 5;
?>
<!DOCTYPE html>
<html>
<head>
<title>Updating Variables</title>
<link rel="stylesheet" href="css/styles.css">
</head>
<body>
<h1>The Candy Store</h1>
<h2>Welcome <?php echo $name; ?></h2>
<p>The cost of your candy is
$<?php echo $price; ?> per pack.</p>
</body>
</html>
A variable can also hold an array which stores a series of related values. Arrays are known as a compound data type because they can store more than one value.
$member = [
'name' => 'Ivy',
'age' => 32,
'country' => 'Italy',
];
<?php
$nutrition = [
'fat' => 16,
'sugar' => 51,
'salt' => 6.3,
];
?>
<!DOCTYPE html>
<html>
<head>
<title>Associative Arrays</title>
<link rel="stylesheet" href="css/styles.css">
</head>
<body>
<h1>The Candy Store</h1>
<h2>Nutrition (per 100g)</h2>
<p>Fat: <?php echo $nutrition['fat']; ?>%</p>
<p>Sugar: <?php echo $nutrition['sugar']; ?>%</p>
<p>Salt: <?php echo $nutrition['salt']; ?>%</p>
</body>
</html>
$shopping_list = ['bread', 'cheese', 'milk',];
<?php
$best_sellers = ['Chocolate', 'Mints', 'Fudge',
'Bubble gum', 'Toffee', 'Jelly beans',];
?>
<!DOCTYPE html>
<html>
<head>
<title>Indexed Arrays</title>
<link rel="stylesheet" href="css/styles.css">
</head>
<body>
<h1>The Candy Store</h1>
<h2>Best Sellers</h2>
<ul>
<li><?php echo $best_sellers[0]; ?></li>
<li><?php echo $best_sellers[1]; ?></li>
<li><?php echo $best_sellers[2]; ?></li>
</ul>
</body>
</html>
Updating Associative array
$member['name'] = 'Tom';
Updating Indexed array
$shopping_list[2] = 'butter';
<?php
$nutrition = [
'fat' => 38,
'sugar' => 51,
'salt' => 0.25,
];
$nutrition['fat'] = 36;
$nutrition['fiber'] = 2.1;
?>
<!DOCTYPE html>
<html>
<head>
<title>Updating Arrays</title>
<link rel="stylesheet" href="css/styles.css">
</head>
<body>
<h1>The Candy Store</h1>
<h2>Nutrition (per 100g)</h2>
<p>Fat: <?php echo $nutrition['fat']; ?>%</p>
<p>Sugar: <?php echo $nutrition['sugar']; ?>%</p>
<p>Salt: <?php echo $nutrition['salt']; ?>%</p>
<p>Fiber: <?php echo $nutrition['fiber']; ?>%</p>
</body>
</html>
<?php
$offers = [
['name' => 'Toffee', 'price' => 5, 'stock' => 120,],
['name' => 'Mints', 'price' => 3, 'stock' => 66,],
['name' => 'Fudge', 'price' => 4, 'stock' => 97,],
];
?>
<!DOCTYPE html>
<html>
<head>
<title>Multidimensional Arrays</title>
<link rel="stylesheet" href="css/styles.css">
</head>
<body>
<h1>The Candy Store</h1>
<h2>Offers</h2>
<p><?php echo $offers[0]['name']; ?> -
$<?php echo $offers[0]['price']; ?> </p>
<p><?php echo $offers[1]['name']; ?> -
$<?php echo $offers[1]['price']; ?> </p>
<p><?php echo $offers[2]['name']; ?> -
$<?php echo $offers[2]['price']; ?> </p>
</body>
</html>