Displaying Script Results
Displaying Script Results
10
11
12
13
Displaying Variables
To print a variable with the echo() statement,
pass the variable name to the echo()
statement without enclosing it in quotation marks:
$VotingAge = 18;
Echo $VotingAge;
14
Displaying Variables
To print text strings and variables, you can send
them to the echo() statement as one argument
enclosed in double quotes:
echo "<p>The legal voting age is $VotingAge.</p>";
The legal voting age is 18.
Modifying Variables
You can modify a variables value at any point in a
script
$SalesTotal = 40;
echo "<p>Your sales total is
$$SalesTotal</p>";
$SalesTotal = 50;
echo "<p>Your new sales total is
$SalesTotal</p>";
16
Defining Constants
A constant contains information that does not
change during the course of program execution
Constant names do not begin with a dollar sign
Constant names use all uppercase letters
Use the define() function to create a constant
define("CONSTANT_NAME", value);
define("VOTING_AGE",18);
define("VOTING_AGE",18,TRUE);
18
19
Boolean Values
A Boolean value is a value of true or false
It decides which part of a program should
execute and which part should compare data
In PHP programming, you can only use true or
false
In other programming languages, you can use
integers such as 1 = true, 0 = false
22
Dynamic Typing
$Variable
$Variable
$Variable
$Variable
$Variable
=
=
=
=
=
"Hello World";
8;
5.367;
TRUE;
NULL;
23
Using Autoglobals
PHP includes various predefined global arrays,
called autoglobals or superglobals
Autoglobals contain client, server, and
environment information that you can use in
your scripts
Autoglobals are associative arrays arrays
whose elements are referred to with an
alphanumeric key instead of an index number
24
25
26
Autoglobals
$_SERVER[PHP_SELF];
$_SERVER[SERVER_SOFTWARE];
$_SERVER[SERVER_PROTOCOL];
$_GET[name];
$_GET[address];
28
Making Decisions
Decision making or flow control is the process
of determining the order in which statements
execute in a program
The special types of PHP statements used for
making decisions are called decision-making
statements or decision-making structures
if Statements
Used to execute specific programming code if
the evaluation of a conditional expression
returns a value of TRUE
The syntax for a simple if statement is:
if (conditional expression)
statement;
if Statements (continued)
Contains three parts:
the keyword if
a conditional expression enclosed within
parentheses
the executable statements
if Statements (continued)
$ExampleVar = 5;
if ($ExampleVar == 5) {
// condition evaluates to 'TRUE'
echo " <p>The condition evaluates to true.</p> ";
echo '<p>$ExampleVar is equal to ',
" $ExampleVar.</p> ";
echo " <p>Each of these lines will be printed.</p> ";
}
echo " <p>This statement always executes after the if
statement.</p> ";
if...else Statements
An if statement that includes an else clause is
called an if...else statement
An else clause executes when the condition in
an if...else statement evaluates to FALSE
The syntax for an if...else statement is:
if (conditional expression)
statement;
else
statement;
if...else Statements
(continued)
An if statement can be constructed without the
else clause
The else clause can only be used with an if
statement
$Today = " Tuesday ";
if ($Today == " Monday ")
echo " <p>Today is Monday</p> ";
else
echo " <p>Today is not Monday</p> ";
switch Statements
Control program flow by executing a specific set
of statements depending on the value of an
expression
Compare the value of an expression to a value
contained within a special statement called a
case label
A case label is a specific value that contains
one or more statements that execute if the value
of the case label matches the value of the switch
statements expression
welcome.php
<html>
<body>
<?php
$name=$_POST["name"];
$email=$_POST["email"];
echo "Your name is ".$name."<br/>";
echo "Your email is ".$email."<br/>";
?>
</body>
</html>
welcome.php
isset Determine if a variable is set and is not NULL
<html>
<body>
<?php
<html>
<body>
<form action="welcome.php" method= "get">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
<html>
<body>
<?php
$name=$_GET["name"];
$email=$_GET["email"];
echo "Your name is ".$name."<br/>";
echo "Your email is ".$email."<br/>";
?>
</body>
</html>
<html>
<body>
<form method="post" action="<?php echo
htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<p>First name: <input type="text" name="firstname" /></p>
<p>Last name: <input type="text" name="lastname" /></p>
<input type="submit" name="submit" value="Submit" />
</form>
</html>
<?php
if(isset($_POST['firstname']) && isset($_POST['lastname']))
{
echo("First name: " . $_POST['firstname'] . "<br />\n");
echo("Last name: " . $_POST['lastname'] . "<br />\n");
}
?>
<?php
if(isset($_POST['firstname']) && isset($_POST['lastname']))
{
echo("First name: " . $_POST['firstname'] . "<br />\n");
echo("Last name: " . $_POST['lastname'] . "<br />\n");
}
?>
<html>
<body>
<form method="post" action="<?php echo
htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<p>First name: <input type="text" name="firstname" /></p>
<p>Last name: <input type="text" name="lastname" /></p>
<input type="submit" name="submit" value="Submit" />
</form>
</html>
Select Forms:
<html>
<body>
<h4>Art Supply Order Form</h4>
<form action="process.php" method="post">
<select name="item">
<option>Paint</option>
<option>Brushes</option>
<option>Erasers</option>
</select>
Quantity: <input name="quantity" type="text" />
<input type="submit" />
</form>
</body>
</html>
process.php
<html>
<body>
<?php
$quantity = $_POST['quantity'];
$item = $_POST['item'];
echo "You ordered ". $quantity . " " . $item . ".<br />";
echo "Thank you for ordering!";
?>
</body>
</html>
<html>
<body>
<h3>PHP HTML Form radio button Example</h3>
<form name="infoForm" method="POST" action=example.php">
Enter Your Full Name :
<input name="FullName" type="text" placeholder="Fullname"><br/><br/>
You are :
<input name="YourGender" type="radio" value="male" > Male
<input name="YourGender" type="radio" value="female" > Female
<br/>
<br/>
<input name="BtnSubmit" type="submit" value="Submit">
</form>
</body>
</html>
example.php
<html>
<body>
<?php
if(isset($_POST['BtnSubmit']))
{
echo "<h3>Your form data as bellow</h3>";
echo "</br>Your Name: {$_POST['FullName']}";
echo "</br>Your are: {$_POST['YourGender']}";
echo "<hr>";
}
?>
</body>
</html>
Checkbox example:
<html>
<body>
<h3>PHP HTML Form checkbox Example</h3>
<form action="process.php" method="post">
<input type="checkbox" name="gender" value="Male">Male</input>
<input type="checkbox" name="gender"
value="Female">Female</input>
<input type="submit" name="submit" value="Submit"/>
</form>
</body>
</html>
process.php
<html>
<body>
<?php
if (isset($_POST['gender']))
{
echo "Your gender is ";
echo $_POST['gender']; // Displays value of checked checkbox.
}
?>
</body>
</html>
<html>
<body>
<h3>PHP HTML Form button Example</h3>
<form name="infoForm" method="POST" action="process.php">
Enter Your Name :
<input name="FullName" type="text" placeholder="Name"><br/><br/>
Enter Your SurName :
<input name="SurName" type="text" placeholder="Surname"><br/><br/>
<input type="submit" name="save" value="Save">
<input type="submit" name="clear" value="Clear">
<input type="submit" name="update" value="Update">
</form>
</body>
</html>
process.php
<html>
<body>
<?php
if (isset($_POST['save']))
{
echo "Save button is pressed! <br /> ";
}
if (isset($_POST['clear']))
{
echo "Clear button is pressed! <br /> ";
}
if (isset($_POST['update']))
{
echo "Update button is pressed! <br /> ";
}
?>
</body>
</html>
</body>
</html>
<?php
$bookArray=$_POST['book'];
echo "Your selected books are <br/>";
foreach ($bookArray as $aBook)
{
echo "$aBook <br>";
}
?>
<html>
<body>
<h3>User Data Form</h3>
<form name="infoForm" method="POST" action= " process.php">
Enter Your Name :
<input name="Name" type="text" placeholder="Name"><br/>
Enter Your SurName :
<input name="Surname" type="text" placeholder="Surname"><br/>
Enter Your Student Number :
<input name="stNumber" type="text" placeholder="Student Number"><br/>
You are :
<input name="YourGender" type="radio" value="male" > Male
<input name="YourGender" type="radio" value="female" > Female
<br/>
<input name="BtnSubmit" type="submit" value="Submit"> <br/><br/>
</form>
</body>
</html>
<?php
$stName=$_POST['Name']; $stSurname=$_POST['Surname']; $stFullName=$Name." ".$Surname;
$stNumber=$_POST['stNumber']; $stGender=$_POST['YourGender'];
$dbhost = "localhost"; $dbuser = "root"; $dbpass = "";
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_select_db('studentInfo');
$sql = "INSERT INTO studentInfoTable (stName, stSurname,stFullName,stNumber, stGender)
VALUES ('$stName','$stSurname','$stFullName','$stNumber','$stGender')";
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
mysql_close($conn);
?>
<?php
$stName=$_POST['Name'];
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_select_db('studentInfo');
$sql = "SELECT * FROM studentInfoTable WHERE stName='$stName'";