What Is PHP?: I (FI Short For "Forms Interpreter")
What Is PHP?: I (FI Short For "Forms Interpreter")
PHP as it's known today is actually the successor to a product named PHP/FI I (FI short for "Forms
Interpreter"). Created in 1994 by Rasmus Lerdorf, the very first incarnation of PHP was a simple set of
Common Gateway Interface (CGI) binaries written in the C programming language. Originally used for tracking
visits to his online resume, he named the suite of scripts "Personal Home Page Tools," more frequently
referenced as "PHP Tools." Over time, more functionality was desired, and Rasmus rewrote PHP Tools,
producing a much larger and richer implementation. This new model was capable of database interaction and
more, providing a framework upon which users could develop simple dynamic web applications such as
guestbooks. In June of 1995, Rasmus released the source code for PHP Tools to the public, which allowed
developers to use it as they saw fit. This also permitted - and encouraged - users to provide fixes for bugs in
the code, and to generally improve upon it.
What is PHP?
● PHP is an acronym for "PHP: Hypertext Preprocessor"
● PHP is a widely-used, open source scripting language
● PHP scripts are executed on the server
● PHP is free to download and use
Page 1 of 21
Chapter 1
Ex:
<?php
// PHP code goes here
?>
A PHP file normally contains HTML tags, and some PHP scripting code.
Below, we have an example of a simple PHP file, with a PHP script that uses a built-in PHP
function "echo" to output the text "Hello World!" on a web page:
<!DOCTYPE html>
<html>
<body>
<?php
echo "Hello World!";
?>
</body>
</html>
Page 2 of 21
Chapter 1
In the example below, all three echo statements below are equal and legal:
<!DOCTYPE html>
<html>
<body>
<?php
ECHO "Hello World!<br>";
echo "Hello World!<br>";
EcHo "Hello World!<br>";
?>
</body>
</html>
Look at the example below; only the first statement will display the value of
the $color variable! This is because $color, $COLOR, and $coLOR are treated as three different
variables:
<!DOCTYPE html>
<html>
<body>
<?php
$color = "red";
echo "My car is " . $color . "<br>";
echo "My house is " . $COLOR . "<br>";
echo "My boat is " . $coLOR . "<br>";
?>
</body>
</html>
Page 3 of 21
Chapter 1
What is a literal?
In computer programming, a literal is the idea of expressing a non-changing value in a
computer program’s source code.
● Number/Decimal .eg 30
Page 4 of 21
Chapter 1
PHP Variables
<?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
?>
After the execution of the statements above, the variable $txt will hold the value Hello
world!, the variable $x will hold the value 5, and the variable $y will hold the value 10.5.
Note: When you assign a text value to a variable, put quotes around the value.
Note: Unlike other programming languages, PHP has no command for declaring a variable.
It is created the moment you first assign a value to it.
PHP Variables
A variable can have a short name (like x and y) or a more descriptive name (age, carname,
total_volume).
● A variable starts with the $ sign, followed by the name of the variable
● A variable name must start with a letter or the underscore character
● A variable name cannot start with a number
● A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9,
and _ )
● Variable names are case-sensitive ($age and $AGE are two different variables)
Page 5 of 21
Chapter 1
Output Variables
The PHP echo statement is often used to output data to the screen.
The following example will show how to output text and a variable:
<?php
$txt = "ISU";
echo "I love $txt!";
?>
Page 6 of 21
Chapter 1
HTML Forms
An HTML form is used to collect user input. The user input is most often sent to a server
for processing.
<form>
.
form elements
.
</form>
The <form> element is a container for different types of input elements, such as: text fields,
checkboxes, radio buttons, submit buttons, etc.
An <input> element can be displayed in many ways, depending on the type attribute.
Type Description
Page 7 of 21
Chapter 1
choices)
Text Fields
The <input type="text"> defines a single-line input field for text input.
<form>
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname">
</form>
Note: The form itself is not visible. Also note that the default width of an input field is 20
characters.
Page 8 of 21
Chapter 1
The <label> element is useful for screen-reader users, because the screen-reader will read
out loud the label when the user focus on the input element.
The <label> element also help users who have difficulty clicking on very small regions (such
as radio buttons or checkboxes) - because when the user clicks the text within
the <label> element, it toggles the radio button/checkbox.
The for attribute of the <label> tag should be equal to the id attribute of
the <input> element to bind them together.
Page 9 of 21
Chapter 1
Radio Buttons
The <input type="radio"> defines a radio button.
<form>
<input type="radio" id="html" name="fav_language" value="HTML">
<label for="html">HTML</label><br>
<input type="radio" id="css" name="fav_language" value="CSS">
<label for="css">CSS</label><br>
<input type="radio" id="javascript" name="fav_language" value="JavaScript">
<label for="javascript">JavaScript</label>
</form>
Page 10 of 21
Chapter 1
Checkboxes
The <input type="checkbox"> defines a checkbox.
Checkboxes let a user select ZERO or MORE options of a limited number of choices.
<form>
<input type="checkbox" id="vehicle1" name="vehicle1" value="Bike">
<label for="vehicle1"> I have a bike</label><br>
<input type="checkbox" id="vehicle2" name="vehicle2" value="Car">
<label for="vehicle2"> I have a car</label><br>
<input type="checkbox" id="vehicle3" name="vehicle3" value="Boat">
<label for="vehicle3"> I have a boat</label>
</form>
Page 11 of 21
Chapter 1
The form-handler is typically a file on the server with a script for processing input data.
<form action="/action_page.php">
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" value="John"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname" value="Doe"><br><br>
<input type="submit" value="Submit">
</form>
If the name attribute is omitted, the value of the input field will not be sent at all.
This example will not submit the value of the "First name" input field:
<form action="/action_page.php">
<label for="fname">First name:</label><br>
<input type="text" id="fname" value="John"><br><br>
<input type="submit" value="Submit">
</form>
Page 12 of 21
Chapter 1
if (condition) {
// Code to be executed if the condition is true
}
Example:
<?php
$age = 20;
if ($age >= 18) {
echo "You are eligible to vote!";
}
?>
Page 13 of 21
Chapter 1
2. else Statement
The else statement is used with if to execute a block of code if the condition of the if
statement is false.
if (condition) {
// Code to be executed if the condition is true
} else {
// Code to be executed if the condition is false
}
Example:
<?php
$age = 15;
if ($age >= 18) {
echo "You are eligible to vote!";
} else {
echo "You are too young to vote.";
}
?>
Page 14 of 21
Chapter 1
3. elseif Statement
The elseif statement allows you to add multiple conditions to the if statement.
if (condition1) {
// Code to be executed if condition1 is true
} elseif (condition2) {
// Code to be executed if condition2 is true
} else {
// Code to be executed if all conditions are false
}
Example:
<?php
$score = 75;
if ($score >= 90) {
echo "You got an A!";
} elseif ($score >= 80) {
echo "You got a B!";
} elseif ($score >= 70) {
echo "You got a C!";
} else {
echo "You need to improve your score.";
}
?>
Page 15 of 21
Chapter 1
4. switch Statement
The switch statement is used to perform different actions based on different conditions.
switch (expression) {
case value1:
// Code to be executed if expression matches value1
break;
case value2:
// Code to be executed if expression matches value2
break;
// You can add more cases as needed
default:
// Code to be executed if there is no match
}
Example:
<?php
$day = "Monday";
switch ($day) {
case "Monday":
echo "Today is Monday.";
break;
case "Tuesday":
echo "Today is Tuesday.";
break;
case "Wednesday":
echo "Today is Wednesday.";
break;
default:
echo "Today is not Monday, Tuesday, or Wednesday.";
}
?>
Page 16 of 21
Chapter 1
The form-data can be sent as URL variables (with method="get") or as HTTP post transaction
(with method="post").
● $_POST
● $_GET
Page 17 of 21
Chapter 1
PHP $_POST
PHP $_POST is a PHP super global variable which is used to collect form data after
submitting an HTML form with method="post". $_POST is also widely used to pass variables.
The example below shows a form with an input field and a submit button. When a user
submits the data by clicking on "Submit", the form data is sent to the file specified in the
action attribute of the <form> tag. In this example, we point to the file itself for processing
form data. If you wish to use another PHP file to process form data, replace that with the
filename of your choice. Then, we can use the super global variable $_POST to collect the
value of the input field:
<html>
<body>
<form method="post" action"#">
Name: <input type="text" name="fname">
<input type="submit">
</form>
<?php
if (empty($name)) {
echo "Name is empty";
} else {
$name = $_POST['fname'];
echo $name;
}
?>
</body>
</html>
Page 18 of 21
Chapter 1
<html>
<body>
</body>
</html>
When a user clicks on the link "Test $GET", the parameters "subject" and "suc" are sent to
"test_get.php", and you can then access their values in "test_get.php" with $_GET.
<html>
<body>
<?php
echo "Study " . $_GET['subject'] . " at " . $_GET['suc'];
?>
</body>
</html>
Notes on GET:
Notes on POST:
● Appends form-data inside the body of the HTTP request (data is not shown in URL)
● Has no size limitations
● Form submissions with POST cannot be bookmarked
Page 19 of 21
Chapter 1
It is said that the web operates on a client-server model, where the client is the web browser
and the server is the computer providing, or serving, the information. That information is
typically stored in a web page, which is nothing more than a specially formatted document that
usually contains images and frequently references to other resources that help the page look
and behave in a certain way.
Web servers and web browsers talk to each other using a protocol called HyperText Transfer
Protocol, or HTTP. In essence, HTTP is just a way for these two parties to speak to each other.
What is PHP?
PHP is a scripting language designed specifically for use on the web, with features that make
web design and programming easier
PHP is a server scripting language, and a powerful tool for making dynamic and interactive
Web pages.
Sending the page to the browser with Apache
PHP and MySQL don’t operate all alone; they need a web server in order to actually respond to
requests for web pages. A web server is special software that runs on a computer. The most
widely used web server on the Internet is httpd from Apache, but most people just refer to it
as “Apache” and so we do the same here. Like PHP, Apache is free.
Page 20 of 21
Chapter 1
Page 21 of 21