PHP Index

In order to become familiar with PHP programming structures and commands, we will build various PHP pages. Sections of this page will explain each PHP command, provide a link to a working sample, and detail the working sample's PHP code. Let's get started:

Variables


Variables are containers for your data. For example, I can say that the variable $student is set equal to "Julio Garcia."

$student="Julio Garcia";

I can then tell PHP to print($student); and PHP will print: Julio Garcia.

print($student);

For example: variables.php

<html>
<head>
<title>variables.php</title>
</head>
<body>
<?php
$student="Julio Garcia";
Print($student);

?>

</body>
</html>

You can also do the following:

$number1=10;
$number2=15;
$number3=5;

print("The sum of ".$number1. ", ".$number2.", and ".$number3. " is:";
print($number1+$number2+$number3);

For example:variables2.php

<html>
<head>
<title>variables.php</title>
</head>
<body>
<?php
$number1=10;
$number2=15;
$number3=5;

$total=$number1+$number2+$number3;

print("<p>The sum of the numbers is ".$total."</p>");

print("I have ".($number1+$number2+$number3)." dollars!");

?>

</body>
</html>




In JavaScript, we used var student to declare the variable. We then set a value to the variable by using student = "Julio Garcia".