ITELEC4 Mod 1
ITELEC4 Mod 1
ITELEC4 Mod 1
IT Major Elective 4
(Web Systems & Development
2)
Module 1
Prepared by:
Kairos Jil I. Molina
molinakairos@gmail.com
Lesso
n PHP
1
What is PHP?
PHP is a general-purpose scripting language especially suited to web development. It was
originally created by Danish-Canadian programmer Rasmus Lerdorf in 1994. The PHP reference
implementation is now produced by The PHP Group. PHP originally stood for “Personal Home
Page”.
PHP code is usually processed on a web server by a PHP interpreter implemented as a module,
a daemon or as a Common Gateway Interface (CGI) executable. On a web server, the result of
the interpreted and executed PHP code – which may be any type of data, such as
generated HTML or binary image data – would form the whole or part of an HTTP response.
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
The PHP language evolved without a written formal specification or standard until 2014, with the original
implementation acting as the de facto standard which other implementations aimed to follow. Since 2014,
work has gone on to create a formal PHP specification.
As of January 2021, 72% of PHP websites use discontinued versions of PHP, i.e., PHP 7.2 or lower,
which are no longer supported by The PHP Development Team. A large additional fraction uses PHP 7.3,
which is only (up to December 6, 2021) "supported for critical security issues only." Over 40% of all PHP
websites use version 5.6 or older, that not even Debian supports (Debian 9 supported version 7.0 and
7.1).
XAMPP is an abbreviation for cross-platform, Apache, MySQL, PHP and Perl, and it allows you to build
WordPress site offline, on a local web server on your computer.
Why PHP?
PHP runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)
PHP is compatible with almost all servers used today (Apache, IIS, etc.)
PHP supports a wide range of databases
PHP is easy to learn and runs efficiently on the server side
Alternative:
Download USBWebserver
https://drive.google.com/file/d/1xuopwuEYtHOlj34yK0qE-dZ3atn36_kw/view?usp=sharing
Place your .php files in the \root folder
Press “Localhost” to view your web app in your browser
Press “PHPMyAdmin” to manage your database
Lesso
n PHP Syntax
2
A PHP script is executed on the server, and the plain HTML result is sent back to the browser.
<?php
// PHP code goes here
?>
<?php
echo "Hello World!";
?>
</body>
</html>
<!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>
Lesso
n PHP Comments
and Variables
3
A comment in PHP code is a line that is not executed as a part of the program. Its only purpose is to be
read by someone who is looking at the code.
<?php
// This is a single-line comment
</body>
</html>
<?php
/*
This is a multiple-lines comment block
that spans over multiple
lines
*/
?>
</body>
</html>
<?php
// You can also use comments to leave out parts of a code line
$x = 5 /* + 15 */ + 5;
echo $x;
?>
</body>
</html>
PHP Variables
Variables are "containers" for storing information.
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.
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)
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.
Sample code:
<?php
$txt = "W3Schools.com";
echo "I love $txt!";
?>
The following example will produce the same output as the example above:
<?php
$txt = "W3Schools.com";
echo "I love " . $txt . "!";
?>
<?php
$x = 5;
$y = 4;
echo $x + $y;
?>
<?php
$x = 5; // global scope
function myTest() {
// using x inside this function will generate an error
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
Example
Variable with local scope:
<?php
function myTest() {
$x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
function myTest() {
global $x, $y;
$y = $x + $y;
}
myTest();
echo $y; // outputs 15
?>