0% found this document useful (0 votes)
11 views

What Is PHP?: I (FI Short For "Forms Interpreter")

The document provides a summary of the origins and early development of PHP. It explains that PHP started in 1994 as a set of CGI binaries created by Rasmus Lerdorf to track visits to his online resume. Over time, it grew to include more functionality and the ability to interact with databases and build simple dynamic web applications. In June 1995, Rasmus released the PHP source code publicly, allowing developers to use and improve upon it.

Uploaded by

JD Dulatre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

What Is PHP?: I (FI Short For "Forms Interpreter")

The document provides a summary of the origins and early development of PHP. It explains that PHP started in 1994 as a set of CGI binaries created by Rasmus Lerdorf to track visits to his online resume. Over time, it grew to include more functionality and the ability to interact with databases and build simple dynamic web applications. In June 1995, Rasmus released the PHP source code publicly, allowing developers to use and improve upon it.

Uploaded by

JD Dulatre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Chapter 1

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

What is a PHP File?


● PHP files can contain text, HTML, CSS, JavaScript, and PHP code
● PHP code is executed on the server, and the result is returned to the browser as plain
HTML
● PHP files have extension ".php"

What Can PHP Do?


● PHP can generate dynamic page content
● PHP can create, open, read, write, delete, and close files on the server
● PHP can collect form data
● PHP can send and receive cookies
● PHP can add, delete, modify data in your database
● PHP can be used to control user-access
● PHP can encrypt data

Page 1 of 21
Chapter 1

Basic PHP Syntax


A PHP script can be placed anywhere in the document.

A PHP script starts with <?php and ends with ?>

Ex:

<?php
// PHP code goes here
?>

The default file extension for PHP files is ".php".

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>

<h1>My first PHP page</h1>

<?php
echo "Hello World!";
?>

</body>
</html>

Note: PHP statements end with a semicolon (;).

Page 2 of 21
Chapter 1

PHP Case Sensitivity


In PHP, keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-defined
functions are not case-sensitive.

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>

Note: However; all variable names are case-sensitive!

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.

Types and examples


Literals can be of almost any type in a programming language, in fact, a literal can
somewhat infer a type(I’ll come back to this later), the types of literals I am aware of are:

● String .eg “Ambrose”

● Number/Decimal .eg 30

● Boolean .eg True/False

● Hexadecimal .eg 0xa1b

Page 4 of 21
Chapter 1

PHP Variables

Variables are "containers" for storing information.

Creating (Declaring) PHP Variables


In PHP, a variable starts with the $ sign, followed by the name of the variable:

<?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).

Rules for PHP variables:

● 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.

The <form> Element


The HTML <form> element is used to create an HTML form for user input:

<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.

The <input> Element


The HTML <input> element is the most used form element.

An <input> element can be displayed in many ways, depending on the type attribute.

Here are some examples:

Type Description

<input type="text"> Displays a single-line text input field

<input type="radio"> Displays a radio button (for selecting one of many

Page 7 of 21
Chapter 1
choices)

<input type="checkbox"> Displays a checkbox (for selecting zero or more of


many choices)

<input type="submit"> Displays a submit button (for submitting the form)

<input type="button"> Displays a clickable button

Text Fields
The <input type="text"> defines a single-line input field for text input.

A form with input fields for text:

<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


Notice the use of the <label> element in the example above.

The <label> tag defines a label for many form elements.

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.

Radio buttons let a user select ONE of a limited number of choices.

A form with radio buttons:

<p>Choose your favorite Web language:</p>

<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.

A form with checkboxes:

<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 Submit Button


The <input type="submit"> defines a button for submitting the form data to a form-handler.

The form-handler is typically a file on the server with a script for processing input data.

The form-handler is specified in the form's action attribute.

A form with a submit button:

<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>

The Name Attribute for <input>


Notice that each input field must have a name attribute to be submitted.

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

PHP Conditional Statements-


if, else, elseif, and switch
1. if Statement
The if statement executes a block of code if a specified condition is true.

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

PHP Global Variables -


Superglobals
Superglobals were introduced in PHP 4.1.0, and are built-in variables that are always
available in all scopes.
But before we go further, let’s discuss HTML forms first (the method attribute).
HTML attributes provide additional information about HTML elements.

HTML <form> method Attribute


Submit a form using the "get" method:
<form action="/action_page.php" method="get">
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname"><br><br>
<label for="lname">Last name:</label>
<input type="text" id="lname" name="lname"><br><br>
<input type="submit" value="Submit">
</form>

Definition and Usage


The method attribute specifies how to send form-data (the form-data is sent to the page
specified in the action attribute).

The form-data can be sent as URL variables (with method="get") or as HTTP post transaction
(with method="post").

PHP Global Variables – Superglobals


Some predefined variables in PHP are "superglobals", which means that they are
always accessible, regardless of scope - and you can access them from any
function, class or file without having to do anything special.

The PHP common superglobal variables are:

● $_POST
● $_GET

Page 17 of 21
Chapter 1

PHP Superglobal - $_POST


Super global variables are built-in variables that are always available in all scopes.

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

PHP Superglobal - $_GET


PHP $_GET
PHP $_GET is a PHP super global variable which is used to collect form data after submitting
an HTML form with method="get".

$_GET can also collect data sent in the URL.

Assume we have an HTML page that contains a hyperlink with parameters:

<html>
<body>

<a href="test_get.php?subject=PHP&suc=ISU ">Test $GET</a>

</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.

The example below shows the code in "test_get.php":

<html>
<body>

<?php
echo "Study " . $_GET['subject'] . " at " . $_GET['suc'];
?>

</body>
</html>

Notes on GET:

● Appends form-data into the URL in name/value pairs


● The length of a URL is limited (about 3000 characters)
● Never use GET to send sensitive data! (will be visible in the URL)
● Useful for form submissions where a user wants to bookmark the result
● GET is better for non-secure data, like query strings in Google

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

Understanding How the Web Works


The World Wide Web consists of a large group of computers, known as servers, that exist solely
to provide information when that information is requested. The information is requested by a
piece of computer software called a web browser.

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.

The web browser


When a client requests a web page, a web browser such as Microsoft Internet Explorer or
Mozilla Firefox (or Safari or Google Chrome or Opera or Lynx) is used. The web page itself can
be a document stored on your computer, just like a word processing document. A program like
Microsoft Word knows how to open documents formatted for Microsoft Word. In the same way, a
web browser knows how to open documents formatted for the web.

The web server


When a web browser requests a page, it typically contacts a web server. Just as the web
browser is software that’s programmed to know how to read and parse web pages, the web server
is software that’s programmed to send web pages when they’re requested.

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.

Setting Up Your Local Computer for Development

Installing the web server


Assuming that you have a computer with an operating system (such as
Windows, Mac OS X, or Linux) already installed, you next need to install a web server. Your
first step is deciding which web server to install. The answer is almost always Apache. Here
are some things to consider, depending on which operating system you’re using:
✦ Windows: Apache provides an installer for Windows that installs and configures Apache for
you.
✦ Linux: Apache is sometimes automatically installed when you install certain Linux
distributions.
✦ Mac: All recent Macs come with Apache installed. However, you might need to install a newer
version of Apache.

Page 20 of 21
Chapter 1

Building PHP Scripts


PHP scripts are a series of instructions in a file named with an extension that tells the web
server to look for PHP sections in the file. (The extension is usually .php or .phtml, but it
can be anything that the web server is configured to expect.) PHP begins at the top of the
file and executes each instruction, in order, as it comes to it.

Instructions, called statements, can be simple or complex.


Complex statements execute one or more blocks of statements. A block of statements consists of
a group of simple statements enclosed by curly braces ({ and }). PHP looks for the ending
curly brace of the last block in complex statements.

PHP Integrated Development Environments (IDE)


PHP integrated development environments, or PHP IDEs, are software platforms that provide programmers and
developers with a comprehensive set of tools for software development in a single product, specifically in the PHP
programming language. PHP IDEs are built to work with specific application platforms and remove barriers involved in
the lifecycle of software development. PHP IDEs are used by development teams to build new software, apps, web pages,
and services, delivering a single tool with all the features needed to accomplish these tasks and removing the need for
integrations. PHP IDEs are used to program code for a specific platform or platforms, and have integrated features
specifically designed for use within these platforms including capabilities to compile, debug, or intelligently complete
code automatically.

Here are the list of IDEs:


● NetBeans
● VS Code
● Eclipse
● Notepad
● Notepad ++

Page 21 of 21

You might also like