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

Web Assignment

Computer Assignments
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Web Assignment

Computer Assignments
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Table of Contents

What is PHP?...............................................................................................................................................2
Why do we learn PHP?................................................................................................................................2
Characteristics of PHP..................................................................................................................................3
In which situations is PHP Applied?.............................................................................................................4
Illustrate the general syntax of PHP............................................................................................................4
Variable Types in PHP..................................................................................................................................5
Operators in PHP.........................................................................................................................................5
Decision making in PHP...............................................................................................................................7
Switch Statement.....................................................................................................................................7
Loops in PHP................................................................................................................................................8
GET and POST Methods...............................................................................................................................9
What are Cookies?......................................................................................................................................9
What is PHP?
Hypertext Preprocessor (PHP) is an open-source, interpreted, and object-oriented scripting
language that can be executed at the server-side. Being an interpreted language, means that there
is no need for PHP code to be compiled before execution. This and other attributes of PHP
makes it faster than other languages. PHP is a language well suited for web application
development. Therefore, many developers us PHP to develop web applications. A web
application is an application that executes on the server and generates the dynamic page.

Why do we learn PHP?


For those who work in web development PHP is an essential language because of the various
features it has. The following are some reasons why people today still learn PHP and prefer it
over other languages. PHP handles dynamic, content, databases as well as session management.
With PHP, you can manipulate information in a database. PHP also tracks the sessions of a user
which helps manage accessibility to pages on the website. This is an important feature because it
allows the developer to design sections of a page that can be accessed by users of different
privilege classes.

PHP script is executed much faster than those scripts which are written in other languages such
as JSP and ASP. PHP uses its own memory, so the server workload and loading time is
automatically reduced, which results in faster processing speed and better performance. Another
reason why we learn PHP is because it is Open source. PHP source code and software are freely
available on the web. You can develop all the versions of PHP according to your requirement
without paying any cost. All its components are free to download and use.

The syntax used in PHP is simple to learn and work with. For someone who has had some
experience programming, the PHP syntax is simple. Even for a person who is new to
programming, understanding the code of PHP is not a very difficult task. PHP also allows us to
use a variable without declaring its data-type. This feature makes it easy to learn and work with.
It will be taken automatically at the time of execution based on the type of data it contains on its
value.

PHP is an ideal language to learn because, unlike other programming languages PHP uses just a
few lines of code to execute the same code that would in other languages be longer. And PHP is
also compatible with many servers including, Apache, Netscape, Microsoft IIS. Because of its
popularity, PHP has a wide community of developers who take time to regularly update
documentations, tutorials and offer online help.

Characteristics of PHP
PHP is an open-source language, this means that the PHP language is made freely available to all
users who can work with it by getting a free PHP license, (www.W3Schools.com). PHP is a
server-side scripting language that is used by many developers to design and distribute both
static and dynamic websites. PHP being open-source means that PHP can be accessed and
modified by anyone who sees fit to use it. Users of PHP can also use the language and its
features at no cost. This fact about PHP makes it a widely used language in the world of web app
development.

PHP is a server-side scripting language. PHP makes it that developers can write code that will be
executed not on the user’s machine, but on the server. PHP code is run on the server and then it
is sent to a client’s browser as a html document. The server creates a HTML document and sends
it back to the client, the browser will then render the made HTML page in the browser. Being a
server-side scripting language makes PHP very ideal for the development of dynamic web pages,
interact with the database and transfer form data entered by the user.

PHP is a server-side scripting language, which means that it runs on the server before the output
is sent to the client’s browser. This makes it easier to create dynamic web pages, process forms,
and interact with databases. Since the code runs on the server, the client cannot view or modify
the code, which improves security. This allows developers to create applications that interact
with databases, store and retrieve data and perform operations like creating tables, adding data,
and modifying data.

PHP can be more functionality by extending it with third-party libraries and extensions. This
important attribute allows developers to use code that is already developed in other libraries,
rather than having to create and develop their own code. Additionally, PHP can also be
combined with HTML, CSS, and JavaScript, making it more adaptable. The large number of
third-party libraries and extensions available for PHP means that developers can find solutions to
common problems quickly and efficiently.
In which situations is PHP Applied?
WordPress is a free and open-source content management system (CMS) based on PHP &
MySQL. It includes a plug-in architecture and template system. It is mostly connected with
blogging but supports another kind of web content, containing more traditional mailing lists and
forums, media displays, and online stores.

Joomla is a free and open-source content management system (CMS) for distributing web
content, created by Open Source Matters, Inc. It is based on a model-view-controller web
application framework that can be used independently of the CMS. Magento is an open source E-
trade programming, made by Varien Inc., which is valuable for online business. It has a flexible
measured design and is versatile with many control alternatives that are useful for clients.

According to (Apiag, Cadiz and Lincopinis, 2023), over 22 billion times a month, Facebook is
accessed, according to Similarweb. Since its creation in 2004, Facebook has benefited from
PHP’s capacity to produce dynamic content in a matter of seconds. By design, Zuckerberg and
Moskowitz wrote all of the PHP code for the core Facebook application. This had a snowball
effect, and now PHP is being used by numerous modern technology firms. In 2010, Facebook
released HipHop for PHP (HPHPc), a transpiler or compiler that converts source code into
source code for another language. HipHop for PHP translates the injected PHP code into C++
before compiling it into an executable binary file.

Illustrate the general syntax of PHP


PHP scripts can be embedded anywhere in the HTML document. PHP is added to HTML using
the PHP tag. The content of PHP has to be embedded within the PHP tag. A PHP script starts
with <?php and ends with ?>. Satements or lines of code in PHP must be terminated with a
semi-colon (;) The following example illustrates how we can use PHP in an HTML document.

<html>
<body>
<h1>My first PHP page</h1>
<?php
echo "Hello World!";
?>
</body>
</html>

Variable Types in PHP


Variables are a means through which we can store information within a program. Variables in
PHP start with a dollar sign ($); and the name has to being with a letter or an underscore.
Variables are case sensitive meaning that they have to be called exactly as they are written.
When assigning a value to a variable we use the equal sign (=); Variables take the value of most
recently assigned value, meaning that the data in a variable can be overwritten. According to
(www.GeeksforGeeks.org), PHP supports the following variables.

Integers, this is a variable type that allows for the storage of numeric values. The numbers that
integers can hold range from -2,147,483,648 and 2,147,483,647. Integers cannot hold numbers
with a decimal point. But they must have at least one digit which can be either positive or
negative. Floats are another variable type that hold numerical data. Floats are also known as
doubles, and unlike integers, floats can hold both whole and decimal numbers.

Strings are variables that are able to store a stream of characters. These characters could be
names, sentences and paragraphs. An example of a string variable is; $sentence = “I am a string”,
strings can also have zero characters, e.g. $string = “”. When variables are written within a
string, it needs double quotes for the value of the string to be printed.

Boolean is another variable type that is supported by PHP. Booleans are designed to hold one of
two values – TRUE or FALSE. The values true or false are used often in loops and condition
statements. Arrays are another variable type supported in PHP. Arrays are types that can hold
multiple values. Each value in an array can be retrieved using the values index. The index of
values in an array start from 0. So when accessing the first value, we use 0 as the index.

Operators in PHP
Operators allows the programmer to perform computations on the data given. These could
including arithmetic operations, comparing of two sets of data and many other operations. PHP
supports the following operators:

Arithmetic Operators
Operator Use
Addition Returns the sum two values. $x + $y
Multiplication Returns the product two values. $x * $y
Modulus Returns the remainder from the division of two values. $x % $y
Decrement Decreases a value by 1. $x - -
Subtraction Returns the difference of two values. . $x - $y
Division Returns the result of a division of two values. $x / $y
Increment Increases a value by 1. $x++

Comparison operators

Comparison operators compare two values with the goal of determining whether they are the
same or not. When comparing values in PHP, the following operators are used. Equal to (= =),
this operator checks to see whether two values are the same. For example $x = 2 and $y = 2.
When comparing the two values we use two equal signs instead of one. $x = = $y, this code
checks whether the two numbers have the same value.

The other comparison operator in PHP is the identical operator (= = =). The identical operator
works just like the equal to operator, with the difference that this operator checks not only the
values, but also the variable type used. So a value that is declared as a float being compared to an
integer would result in false, because the types are different. For example, $x = 2 and $y = 2.0,
these two numbers are not the same because $y is a float and $x is an integer.

The greater than (>) and less than (<) symbols along with the greater than or equal to (>=) and
greater than or equal to (<=) are also used to compare values. These operators are designed to
check whether one value is bigger or less than the other value. These operators are often used in
loops to help determine the number of iterations of a loop.

In PHP logical operators are designed to return either a True or false value. The operators that
are supported in PHP are as follows. The Logical AND operator (&&), this operator returns
TRUE if both of the values being checked evaluate as true. If any of the values is false, the AND
operator will return false. The Logical OR operator (| |), if one or both of the values are true, the
OR operator returns evaluates as true. The NOT operator (!), inverts a value, this means that if a
value was true, it will be inverted to false.

Decision making in PHP


When a developer needs the program to make a decision based on the information given,
decision statements are implemented to achieve this, (www.GeeksforGeeks.org). The following
are the decision statements that are supported in PHP:

The If statement executes code if the condition is true. The following is an example of how we
can use the If statement in PHP.

<?php
$x = 1;
If($x >= 5){
echo “Your number is bigger that 5”;
}
?>
The if else statement evaluates the if part of the code if the condition is true, if the condition
evaluate false the else part of the statement will evaluate. The following is an example of how we
use the if … else statement:
<?php
$whether = warm;
If($whether = = “warm”){
echo “Today is a hot day”;
} else {
Echo “Today is cold”;
}
?>
Switch Statement
The switch statement works similar to the if … else … if statement. This statement has a list of
cases that it runs through until it find the one that is true. The following example, illustrates the
switch statement, (www.W3Schools.com).
<?php
$hour = 8;
switch ($hour){
case >= 6 && <=12:
echo “Good Morning!”;
break;
case >= 13 && <=17:
echo “Good Afternoon!”;
break;
case >= 18 && <=22:
echo “Good Night!”;
break;

default:
echo “It is too late people are sleeping!”;
}
?>

Loops in PHP
Loops are a means through which a programmer can have the same piece of code run over and
over for a set number of times without having to redundantly write the same line of code. The
following are the loops that are supported in PHP:
While Loop
The while loop continues executing the same piece of code for as long as the set condition is not
met. The following code shows an example of how we can use a While Loop,
(www.GeeksforGeeks.org):
<?php
$x = 0;
while ($x <5) {
echo “Your number is: $x;
$x++;
}
?>
Do … While Loop, (www.W3Schools.com)
This loop runs at least once, the loop only checks the condition after the first execution, the do
while loop is the only loop that guarantees the execution of code before evaluating the condition.
<?php
$x = 0;
do {
echo “Your number is: $x;”;
$x++;
} while ($x <5);
?>
For Loop, (www.W3Schools.com)
The for loop in PHP executes a block of code for a specified amount of times.
<?php
for($x = 0; $x <5; $x++){
echo “Your number is: $x”;
} ?>
Foreach Loop, (www.W3Schools.com)
The foreach loop is a special loop that works only on arrays. This loop runs through an array
making key/value pairs as it executes,
<?php
$cars = array(“Vitz”, “Allion”, “Corolla” , “Hilux”);
foreach($cars as $car){
echo “$Your car is: $car \n”;
}
?>

GET and POST Methods


PHP has two superglobal variables that are used when sending data from a form to the server,
these are the POST and GET methods. When a user enters data on a form, the data needs to be
sent to the server for storage or for further processing. Developers use the POST and GET
methods to achieve this.

The GET method is a superglobal whose data is sent directly through the URL. When this
method is used, the data captured on a form from the user is appended to the URL. Because data
is attached to the URL this method is not ideal for sending data such as passwords and other
sensitive information. This feature of the GET methods alsoe limits the amount of data that can
be transmitted to the server because of the limit imposed on the URL.
(www.GeeksforGeeks.org).

The POST method is used like the GET method to transfer data to the server, but this method is
different in that data is not attached to the URL. Data is sent separately through the HTTP body
rather than be attached to the URL. This attribute makes the POST ideal for transmitting
sensitive information. This also means that large amounts of data can be transferred because
there are not URL size restrictions on method.

What are Cookies?


Cookies are a small program that are sent by the server to a client’s browser. The browser once it
receives cookies from the server will keep save them and later send them to the server when they
are needed. Cookies store frequently used instruction and user preferences such as colors and
fonts. Cookies also track activities on the web and can later suggest (predict) future steps when a
person is on the web.
REFERENCES

Apiag, C., Cadiz, E. and Lincopinis, D. (2023). A Review on PHP Programming Language.
Western Mindanao State University.
Toal, R. (2022). What is PHP? Retrieved from: https://codeinstitute.net/global/blog/what-is-
phpprogramming
Trachtenberg, A. & Sklar, D. (2021) Scaling PHP Applications. Retrieved from:
https://www.oreilly.com/library/view/php-cookbook-2nd/
TutorialsPoint, (2016). PHP: Hypertext Preprocessor. Retrieved from:
https://store.tutorialspoint.com/
(www.GeeksforGeeks.org). PHP Tutorial. Retrieved from: https://www.geeksforgeeks.org/php-
tutorial/
(www.W3Schools.com). PHP Tutorial. Retrieved from: https://www.w3schools.com/php/

You might also like