Module 1 2 Web System
Module 1 2 Web System
QUIAMBAO
C. CONTENT
processing instructions <?PHP and ?> that allow you to jump into and out of
"PHP mode." Figure 1.2 Sample PHP Script
The best things in using PHP are that it is extremely simple for a newcomer, but offers many advanced features for a professional
programmer.
3|Web System and Technologies 2
Learning Module |Prepared by: MARK JOSHUA V. QUIAMBAO
Through the years, PHP is the premium choice of developers for developing web applications. Thanks to Rasmus Lerdorf, who back in
1994, conceived the idea of PHP. Working through it, he later made its first-ever version public in 1995 known as PHP/FI. At that time,
it was just a simple PERL/CGI script toolset written in C language. The language evolved quickly after the release of PHP3 in 1998,
which included several advanced features – most importantly – the introduction of the Zend engine. Today, PHP has found its usage in
many leading organizations for handling their online portfolio. These big guns include Facebook, Wikipedia, Yahoo, WordPress and
others, while the numbers are still growing.
PHP has a lot of advantages and some are described above. It has disadvantages as well but it is still better than other programming
languages. It is being widely used the programming language and many people are using this programming language for creating the
different types of a web-based application. It is used because of its features and open source. It does not cost and able to develop the
customized application integrated with other programming languages as well.
Following are the top 10 advantages of PHP which are as follows:
1. Open Source
PHP is open-source and free of cost, which helps developers to install it quickly and readily available for use. There are a lot of PHP
frameworks and developer can choose any of the frameworks to work. All the features and tools will be provided to the developer for
that framework easily. As it is open-source, it makes the system ready with PHP in quick time and makes the web development faster
with the help of providing the tools and other features easily.
2. Platform Independent
PHP is mainly supported by all the operating systems like Windows, UNIX, Linux, etc. The PHP based developed web applications can
be easily run on any platform. It can be integrated with other programming language and database easily and there is no requirement
of re-development. It helps in saving a lot of effort and cost.
3. Simple and Easy
This advantage of PHP is simple and easy to learn and code. It is mainly organized code and clean, which helps the new developers
also. The command functions of PHP can easily learn and understood. The one who knows any programming language can easily
work on PHP. It is simple to learn, as its learning curve is not large. The syntax is simple and flexible to use.
4. Database
PHP is easily connected with the database and make the connection securely with databases. It has a built-in module that is used to
connect to the database easily. There are many web applications, which require strong programming language with a good database
management system. PHP and its database connection solve the purpose for the development of web applications. It reduces the time
to connect to a database management system as well. Multiple databases can be integrated with PHP.
5. Fast
PHP is known as the fastest Programming language as compared to another. PHP applications can be easily loaded over the slow
Internet and data speed. Other applications take a lot of time to connect the database and fetch the data after executing certain queries
to the database. PHP does not face this problem and it loads the website very easily and fast. The fast speed of PHP provides the
developer with an edge to develop the web applications in PHP programming language.
6. Maintenance
PHP framework is mainly used to make the web application development easier and maintain the code automatically. The model view
controller architecture in PHP framework helps the code to be easily maintained and used. The MVC architecture helps the separation
of a file for different module separately.
7. Support
This advantage of PHP has great online support and community, which helps the new developers to help in writing the code and
developing the web applications. The documentation provided at the official site helps in using the different features of PHP and its
framework. The latest updates are released timely by the PHP to make it better for the developer to develop the web-based
applications.
8. Testing
PHP based web applications can be easily tested. PHP unit uses to perform the unit testing quickly and easily. It also helps the
programmers to write test cases and perform the testing smoothly. For PHP based web applications, the developers do not need to
write the additional code. PHP frameworks help in automating the different tasks.
9. Security
PHP frameworks built-in feature and tools make it easier to protect the web applications from the outer attacks and security threats.
The security threats can be like SQL injection, data tampering, and forgery etc. To protect from these security threats, developers used
PHP frameworks for developing web applications.
10. Stable
PHP is also stable as compared to other programming languages. It has been in existence for a long time. The developers have
worked on PHP to make it easy for the programmers to work on developing the PHP web-based applications. They have fixed the
issues and bugs over the period for the different version of PHP and make it very stable.
What are the applications of PHP?
There are wide applications of PHP in several digital platforms. Now we are going to provide you with some of the important
applications of PHP programming language in different fields with their brief information.
1. Web Pages and Web-Based Applications:
Any web page or web application developed in the current ecosystem needs to offer a high degree of customization, provide a highly
interactive user interface, be capable of performing online transactions and integrate with database systems. Through its three-tiered
architecture which works on browser, server and database system linearly, PHP offers a reliable solution to achieve these features. As
a consequence, PHP is used by over 82% of websites for server-side programming; over 244 million websites are built using PHP.
Further, web-based enterprise applications and a multitude of Facebook apps are also scripted in PHP.
2. Web Content Management Systems:
PHP offers support to numerous databases including MySQL, Oracle and MS Access and is capable of interacting with other services
using protocols such as IMAP, LDAP, POP3, NNTP, SNMP, HTTP and COM . As a result, PHP has been used for creating popular
web Content Management Systems (CMS), including WordPress and its plugins, user-facing portion of Facebook, Joomla, Drupal,
Moodle, Silverstripe, MediaWiki, Digg and others.
3. eCommerce Applications:
From small businesses to large enterprises, selling to online shoppers is now an indispensable requirement. Through use of
frameworks like CodeIgniter and CakePHP, PHP allows creation of eCommerce applications in a swift and simple manner. Many of the
highly used eCommerce platforms, such as OpenCart, Magento, PrestaShop, Zen Cart, AgoraCart and Ubercart, have all been created
on PHP.
4. GUI-Based Applications:
While PHP is largely used as a scripting language for web-based applications, it is also possible to employ it for creating desktop
graphical user interface (GUI) based applications. Tools like PHP-GTK 2, DevelStudio and ZZEE PHP GUI, allow scripting in PHP,
compiling it into .exe capable of running standalone.
5. Create Flash:
An open source library, such as Ming, helps to create .SWF format movies which include most of the features of Flash – gradients,
bitmaps (pngs and jpeg), morphs, shapes, text, button, actions and animations. Further, Flash elements like login-form and email form
can be easily created and incorporated into dynamic web pages using PHP.
6. Image Processing and Graphic Design:
Apart from managing text content, PHP can also be used to manipulate images. Integration of various image processing libraries, such
as GD library, Imagine and ImageMagick, in PHP applications allows rotating, cropping, resizing, creating thumbnail pictures, adding
watermarks and generating output images in multiple formats including jpeg, gif, wbmp, xpm and png. This feature is an essential
requirement for building robust websites and web applications.
7. Data Representation:
Through use of tools like Image_Graph, PHP can be used to create a variety of graphs, charts, scatter-dot plots and other means of
graphical representations. This feature finds application across client-side desktop applications, eCommerce websites and other
applications where data needs to be presented in a concise and graphical manner.
8. Creating PDF Files:
Further, by use of a library called PDFLib, PHP can be used to generate PDF files. This can facilitate the creation of an online invoicing
system where an HTML-driven invoice is created in PDF format.
1.2 Introducing XAMPP and WAMPP
What you need to run PHP?
To run PHP code, you need the following three software on your local machine:
1. Web Server. A web server is a computer that runs websites. It's a computer program that distributes web pages as they are
requisitioned. The main job of a web server is to display the website content. The most commonly used web server is Apache,
an open-source and free web server software that powers around 40% of websites around the world. The official name is
Apache HTTP Server, and it’s maintained and developed by the Apache Software Foundation.
2. PHP (Interpreter). PHP is an interpreted language. This means that you will write code statements (lines of code) and when a
page is requested, the PHP interpreter will load your PHP code, parse it and then execute it.
3. Database Software (optional). Database software is designed to create databases and to store, manage, change, search, and
extract the information contained within them. A comprehensive database software program is sometimes called a database
management system. MySQL is the most popular database system used with PHP.
You can separately install Web Server, PHP Interpreter, and databases, but to make work easier, developers have made all in one
setup package that will automatically install and set up PHP environment on your Windows, Linux or MAC machines. There following
are the four common web server solution package used:
Figure 1.5 Official Website where you can download the latest
version of WAMP.
6.
3. Click Yes when prompted. This will open the XAMPP setup window. You may have to click OK on a warning if you have User
Account Control (UAC) activated on your computer. The
click Next.
2. Click Start buttons next to Apache and MySQL. This starts the personal web service or Apache and MySQL.
3. Click Admin next to Apache. If everything is working properly, you should see the XAMPP Dashboard. You can click one of
the icons at the bottom of the screen to see a list of additional modules you can install and use with XAMPP. These include
WordPress, Drupal, Joomla, Mautic, OpenCart, OwnCloud, phpList, phpBB, and more. Alternatively, you can go to
http://localhost/ in a web browser.
4. Click Admin next to "MySQL". This opens the phpMyAdmin dashboard. Here you can configure your PHP databases.
If in case your Operating System is not Windows I provided some Youtube link below.
https://www.youtube.com/watch?v=_hmNWQtl1bo
https://www.youtube.com/watch?v=EK_AUTzV7OI
declaration blocks, so you must be able to recognize the other delimiters when you them.
What is the syntax of the other delimiters?
Figure 1.25 Using ‘echo’ statement
The <script> element
When the script element is used with PHP, you must assign a value of “php” to the
<script language="php">
language attribute of the <script> like what shown in Figure 1.26. Like the standard
echo "Hello World!"; PHP script delimiters, the <script> element is always available on any Web server
</script> that support PHP. Unfortunately, the <script element’s language attribute
deprecated in XHTML, as a result you cannot validate documents that include PHP
Figure 1.26 Sample code using <script> <script> elements.
element
Short PHP Script Delimiters
A simplified method of writing PHP code declaration blocks is to use the short
<?
script like what shown in Figure 1.27. Although it is simple and short, the major
echo "Hello World!"; drawback of this delimiter is it is not available in all webserver unlike the standard
?> and <script>. That’s why the PHP Group discourages the use of short delimiters,
Figure 1.27 Sample Short PHP script especially when it redistributed and used by others.
ASP-Style Script Delimiters
The ASP-style delimiters is similar to short PHP, the only difference is the
<% question mark was replaced by percent symbol like what shown in Figure 1.28.
echo "Hello World!"; ASP-style script can also ne enabled or disabled in the php.ini configuration file,
%> so you should not use them unless you are sure they are enabled on any Web
Figure 1.28 Sample ASP-style script servers on which PHP script will run.
D. Learning Activity
Activity No. 1
Direction: Make your own tutorial video of WAMP or XAMPP installation. You will be graded based on the rubrics provided.
E. Assessment
REFERENCES
https://techdifferences.com/difference-between-server-side-scripting-and-client-side-scripting.html
https://www.fasthosts.co.uk/blog/client-side-vs-server-side-scripting/
https://www.php.net/manual/en/intro-whatis.php
https://www.educba.com/advantages-of-php/
https://www.cloudways.com/blog/php-version-history/
https://www.invensis.net/blog/applications-php-programming-language/
https://www.w3trainingschool.com/applications-php-programming-language
https://economictimes.indiatimes.com/definition/web-server
https://www.w3schools.in/php/install/
https://www.hostinger.ph/tutorials/what-is-apache
https://stillat.com/blog/2014/04/02/how-does-php-work-with-the-web-server-and-browser
https://www.quickbase.com/articles/database-software-basics
https://www.w3schools.com/php/php_mysql_intro.asp
https://blog.templatetoaster.com/how-to-install-wamp/
https://www.wikihow.com/Install-WAMP
https://www.w3schools.com/php/php_syntax.asp
https://www.apachefriends.org/download.html
https://www.wikihow.com/Install-XAMPP-for-Windows
https://www.wikihow.com/Set-up-a-Personal-Web-Server-with-XAMPP
https://tutorialsclass.com/faq/is-php-a-case-sensitive-language/
Gosselin, D., Kokoska, D., & Easterbrooks, R., 2011, “PHP Programming with MySQL”. Cengage Learning
Activity Rubrics
Doubles (also known as float) − are floating-point numbers, like 3.14159 or 49.1.
Arrays can also associate a key other than an integer index to a value. In PHP, all arrays are associative arrays behind the scenes, but
when we refer to an 'associative array' distinctly, we usually mean one that contains one or more keys that aren't integers.
Objects − are instances of programmer-defined classes, which can package up both other kinds of values and functions that
are specific to the class.
Resources − are special variables that hold references to resources external to PHP (such as database connections).
The first five are simple types, and the next two (arrays and objects) are compound - the compound types can package up other
arbitrary values of arbitrary type, whereas the simple types cannot.
Type Strength
PHP is called weakly typed, or dynamically typed language. In most programming languages, variables can hold only one type of data,
and that type must be declared before the variable can be used, as in C. In PHP, the type of a variable is determined by the value
assigned to it.
For example, you have a variable $total you assigned a value of “0”, just what you see below.
$total=0;
The PHP distinguished its datatype as integers since you assigned a whole numeric value which is zero (0). Let say you realized that
the $total might not always whole number so you changed the value as “0.00” like what shown below.
$total=0.00;
PHP will categorized it now as double since it has decimal point. On the other hand, when you want to define a string variable type you
need to have single or double quotation like what shown below.
<?php
$username= ‘cuteako123’; Output:
$user= “My user name is $username”;
cuteako123
echo $username; My user name is cuteako123
echo $user;
?>
This ability to change types transparently on the fly can be extremely useful. Remember PHP "automagically" knows what data type
you put into your variable. It returns the data with the same data type when you retrieve it from the variable.
Type Casting
You can pretend that a variable or value is of a different type by using a type cast. This feature works identically to the way it works in
C. You simply put the temporary type in parentheses in front of the variable you want to cast. For example, you could have declared the
two variables from the preceding section using a cast:
$total 0;
$totalamount = (float) $total;
The second line means "Take the value stored in $total, interpret it as a float, and store it in $totalamount." The $total amount variable
will be of type float. The cast variable does not change types, so $total remains of type integer.
You can also use the built-in function to test and set type, which you will learn about later in this chapter.
Variable Variables
PHP provides one other type of variable: the variable variable. Variable variables enable you to change the name of a variable
dynamically. As you can see, PHP allows a lot of freedom in this area. All languages enable you to change the value of a variable, but
not many allow you to change the variable's type, and even fewer allow you to change the variable's name. A variable variable works
by using the value of one variable as the name of another For example, you could set
$varname = 'total';
You can then use $$varname in place of $total. For example, you can set the value of $total as follows:
$$varname = 5;
This approach might seem somewhat obscure, but we'll revisit its use later. Instead of having to list and use each form variable
separately, you can use a loop and variable to process them all automatically. This will be discuss on for loops later on this module.
As you saw previously, you can readily change the value stored in a variable. You can also declare constants. A constant stores a
value just like a variable, but its value is set once and then cannot be changed elsewhere in the script. In the sample application, you
might store the prices for each item on sale as a constant. You can define these constants using the define function:
27|Web System and Technologies 2
Learning Module |Prepared by: MARK JOSHUA V. QUIAMBAO
One important difference between constants and variables is that when you refer to a constant, it does not have a dollar sign in front of
it. If you want to use the value of a constant, use its name only. For example, to use one of the constants just created, you could type
Output:
echo Pi; 3.1416
As well as the constants you define, PHP sets a large number of its own. An easy way to obtain an overview of them is to run the
phpinfo() function:
phpinfo ():
This function provides a list of PHP's predefined variables and constants, among other useful information. We will discuss some of
them as we go along. One other difference between variables and constants is that constants can store only Boolean, integer, float, or
string data. These types are collectively known as scalar values.
Variables inside functions that are declared as global refer to the global variables of the same name.
Variables created inside functions and declared as static are invisible from outside the function but keep their value between
one execution of the function and the next.
Variables created inside functions are local to the function and cease to exist when the function terminates.
The arrays $_GET and $_POST and some other special variables have their own scope rules. They are known as superglobals or
autoglobals and can be seen everywhere, both inside and outside functions.
The complete list of superglobals is as follows: $GLOBALS-An array of all global variables (Like the global keyword, this allows you to
access global variables inside a function-for example, as
$_GLOBALS[ 'myvariable'1.)
$_SERVER-An array of server environment variables
$_GET-An array of variables passed to the script via the GeT method
$_POST-An array of variables passed to the script via the PoST method
$_COOKIE-An array of cookie variables
$_FILES-An array of variables related to file uploads
$_ENV-An array of environment variables
$_REQUEST-An array of all user input including the contents of input including
$_GET, $_POST, and $_COOKIE (but not including $_FILES since PHP 4.3.0)
$_SESSION-An array of session variables
We come back to each of these superglobals throughout the module as they become relevant. We cover scope in more detail when we
discuss functions and classes later in this chapter. For the time being, all the variables we use are global by default.
echo and print- The echo and print are language constructs, not functions. This means that they don't require parentheses
around the argument like a function does (although one can always add parentheses around almost any PHP expression and
thus echo("test") won't do any harm either). They output the string representation of a variable, constant, or expression. They
can't be used to print arrays or objects.
When outside of PHP tags, a shorthand notation for echo is available by default, using <?= to begin output and ?> to
end it. For example:
Note that there is no terminating ;. This works because the closing PHP tag acts as the terminator for the single statement. So, it is
conventional to omit the semicolon in this shorthand notation.
Priority of print
Although the print is language construction it has priority like operator. It places between = += -= *= **= /= .= %=
&= and and operators and has left association. Example:
Using Operators
Operators are symbols that you can use to manipulate values and variables by pertorming an operation on them.
30|Web System and Technologies 2
Learning Module |Prepared by: MARK JOSHUA V. QUIAMBAO
Arithmetic Operators-The PHP arithmetic operators are used with numeric values to perform common arithmetical operations, such as
addition, subtraction, multiplication etc.
Operator Name Example Result
Assignment Operators- The PHP assignment operators are used with numeric values to write a value to a variable. The basic
assignment operator in PHP is "=". It means that the left operand gets set to the value of the assignment expression on the right.
Assignment Same as... Description
x=y x=y The left operand gets set to the value of the expression on the right
x += y x=x+y Addition
x -= y x=x-y Subtraction
x *= y x=x*y Multiplication
x /= y x=x/y Division
x %= y x=x%y Modulus
Comparison Operators-The PHP comparison operators are used to compare two values (number or string):
Operator Name Example Result
=== Identical $x === $y Returns true if $x is equal to $y, and they are of the same type
!== Not identical $x !== $y Returns true if $x is not equal to $y, or they are not of the same type
>= Greater than or equal to $x >= $y Returns true if $x is greater than or equal to $y
<= Less than or equal to $x <= $y Returns true if $x is less than or equal to $y
<=> Spaceship $x <=> $y Returns an integer less than, equal to, or greater than zero, depending
on if $x is less than, equal to, or greater than $y. Introduced in PHP 7.
Increment / Decrement Operators-The PHP increment operators are used to increment a variable's value while decrement operators
are used to decrement a variable's value.
Operator Name Description
=== Identity $x === $y Returns true if $x and $y have the same key/value pairs in the same order
and of the same types
Conditional Assignment Operators-The PHP conditional assignment operators are used to set a value depending on conditions:
Operator Name Example Result
PHP string is a sequence of characters i.e., used to store and manipulate text. PHP supports only 256-character set and so that it does
not offer native Unicode support. There are 4 ways to specify a string literal in PHP.
Single Quoted-We can create a string in PHP by enclosing the text in a single-quote. It is the easiest way to specify string in
PHP.
For Example
For specifying a literal single quote, escape it with a backslash (\) and to specify a literal backslash (\) use double backslash (\\). All the
other instances with backslash such as \r or \n, will be output same as they specified instead of having any special meaning.
We can store multiple line text, special characters, and escape sequences in a single-quoted PHP string.
Double Quoted-In PHP, we can specify string through enclosing text within double quote also. But escape sequences and
variables will be interpreted using double quote PHP strings.
Now, you can't use double quote directly inside double quoted string.
We can store multiple line text, special characters and escape sequences in a double quoted PHP string.
Heredoc-Heredoc syntax (<<<) is the third way to delimit strings. In Heredoc syntax, an identifier is provided after this heredoc
<<< operator, and immediately a new line is started to write any text. To close the quotation, the string follows itself and then
again that same identifier is provided. That closing identifier must begin from the new line without any whitespace or tab.
Naming Rules-The identifier should follow the naming rule that it must contain only alphanumeric characters and
underscores, and must start with an underscore or a non-digit character.
For Example
Valid Example
Invalid Example
We cannot use any whitespace or tab before and after the identifier and semicolon, which means identifier must not be indented. The
identifier must begin from the new line.
Heredoc is similar to the double-quoted string, without the double quote, means that quote in a heredoc are not required. It can also
print the variable's value.
Example
Newdoc- Newdoc is similar to the heredoc, but in newdoc parsing is not done. It is also identified with three less than symbols
<<< followed by an identifier. But here identifier is enclosed in single-quote, e.g. <<<'EXP'. Newdoc follows the same rule as
heredocs.
The difference between newdoc and heredoc is that - Newdoc is a single-quoted string whereas heredoc is a double-quoted string.
Example
Example below shows that newdoc does not print the variable's value
Output:
Invalid Example
We cannot use any whitespace or tab before and after the identifier and semicolon, means identifier must not be indented. The
identifier must begin from the new line. It is also invalid in newdoc same as heredoc.
The PHP string functions are part of the PHP core. No installation is required to use these functions.
Function Description
chop() Removes whitespace or other characters from the right end of a string
hebrevc() Converts Hebrew text to visual text and new lines (\n) into <br>
ltrim() Removes whitespace or other characters from the left side of a string
rtrim() Removes whitespace or other characters from the right side of a string
strchr() Finds the first occurrence of a string inside another string (alias of strstr())
strcspn() Returns the number of characters found in a string before any part of some specified characters are
found
stripos() Returns the position of the first occurrence of a string inside another string (case-insensitive)
stristr() Finds the first occurrence of a string inside another string (case-insensitive)
strpos() Returns the position of the first occurrence of a string inside another string (case-sensitive)
strripos() Finds the position of the last occurrence of a string inside another string (case-insensitive)
strrpos() Finds the position of the last occurrence of a string inside another string (case-sensitive)
strspn() Returns the number of characters found in a string that contains only characters from a specified charlist
strstr() Finds the first occurrence of a string inside another string (case-sensitive)
substr_compare() Compares two strings from a specified start position (binary safe and optionally case-sensitive)
D. Learning Activities
Activity 2.1
Write a PHP script to get the PHP version and configuration information.
Activity 2.2
Write a PHP script to display the strings “I am a PHP developer” with variable name $me.
Activity 2.3
$var = 'PHP Tutorial'. Put this variable into the title section, h3 tag and as an anchor text within an HTML document.
Activity 2.4
Create a simple HTML form and accept the user name and display the name with a greeting “Hello” through PHP echo statement.
Sample Output
Activity 2.5
Make a PHP script that make simple calculator
Activity 2.5
Make a PHP script that can perform simple addition.
Sample Output
E. Assessment
1. Which of the variables is float?
a. $qty = “1.01”; c. $qty = ‘1.01’;
b. $qty = 1; d. $qty = 1.001;
45|Web System and Technologies 2
Learning Module |Prepared by: MARK JOSHUA V. QUIAMBAO
REFERENCES
June Jamrich Parson and Dan O. (2014). New Perspective on Computer Concepts, Cengage Learning
https://www.tutorialspoint.com/php/php_variable_types.htm
https://www.w3schools.com/php/php_operators.asp
https://www.javatpoint.com/php-string
ACTIVITY RUBRICS