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

PHP Assigment

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

PHP Assigment

PhP assignment, summary
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Table of Contents

What is PHP?..............................................................................................................................................2
Why learn PHP?..........................................................................................................................................2
Advantages of PHP.....................................................................................................................................3
Five Characteristics of PHP.........................................................................................................................3
Open Source............................................................................................................................................3
Cross Platform.........................................................................................................................................3
Easy to Learn...........................................................................................................................................4
Object Oriented Programming.................................................................................................................4
Security...................................................................................................................................................4
Where can PHP be applied?........................................................................................................................4
Dynamic Web Pages................................................................................................................................4
User Authentication and Session management........................................................................................5
Prerequisites of PHP....................................................................................................................................5
General Syntax of PHP.................................................................................................................................5
Variable Types in PHP................................................................................................................................6
Operator Types in PHP................................................................................................................................6
Arithmetic Operators...............................................................................................................................7
Logical Operators.....................................................................................................................................7
Comparison Operators............................................................................................................................7
Decision Making in PHP...............................................................................................................................7
If Statement.............................................................................................................................................8
If … else statement..................................................................................................................................8
Switch Statement....................................................................................................................................8
PHP Loops....................................................................................................................................................9
GET and POST Methods.............................................................................................................................10
What are Cookies?.................................................................................................................................10
What is PHP?
PHP (Hypertext Preprocessor) is a popular language for web development. Its popularity is
mainly due to its ease of use and versatility. PHP is a scripting language that runs on servers and
its popular use is web development, (Apiag, Cadiz and Lincopinis, 2023). PHP is an interpreted
language, meaning when executing PHP code, there is no need for compilation. PHP is generally
faster than other scripting languages such as JavaScript.

PHP was first released in 1995 by Rasmus Lerdorf, and since then, it has continued to evolve and
improve, (Apiag, Cadiz and Lincopinis, 2023). Andersen, (2020), states that one of the key
benefits of PHP is its flexibility. As an open-source language, PHP has a large and active
community of developers who contribute to its development and create useful tools and libraries
for it. PHP code can also be embedded into HTML and it runs smoothly.

Why learn PHP?


PHP is widely taught because of it wide use. PHP is widely compatible with all servers that are
available today. A web server is an information technology that processes requests via HTTP, the
basic network protocol used to distribute information on the World Wide Web. There are many
types of web servers that servers use. Some of the most important and well-known servers are:
Apache HTTP Server, IIS (Internet Information Services), Sun Java System Web Server etc.
PHP is compatible with all these web servers and many others that are used in the world of
networks.

PHP is taught because of its ability to run on any platform. Unlike other languages that require
code for different platforms, PHP able to run on platforms such as Windows, Linux, Mac OSX,
Unix and many other platforms like these. Another important reason why PHP is learnt is
because is its relationship with various databases. Some of these databases are: DB++, dBase,
Ingres, Mongo, MaxDB, MongoDB, mSQL, Mssql, MySQL, OCI8, PostgreSQL, SQLite,
SQLite3 and so on, (www.WebcodeforGeeks.com).

PHP is also free for all customers. Anyone can start using PHP by downloading it from php.net,
(www.WebcodeforGeeks.com). Millions of people are using PHP to create dynamic content and
database-related applications that make for outstanding web systems. PHP is also open source,

1|Page
which means the original source code is made freely available and may be redistributed and
modified.

PHP is easy to learn. One of the main reasons PHP became so commonplace is that it is
relatively simple to get started with. Even without extensive knowledge or experience in web
development, most people could create a web page with a single PHP file in a relatively short
period of time. The syntax is simple, and command functions are easy to learn, meaning the
barriers to entry with PHP are lower than with many other languages,
(www.WebcodeforGeeks.com).

PHP is known to be an efficient language, especially with the release of PHP 7. With
improvements in memory usage and CPU time reduction, PHP 7 provides faster
execution times compared to earlier versions. Additionally, PHP has a large number
of built-in functions and extensions that allow developers to optimize their code and
improve performance. Overall, PHP is a performance language suitable for building
scalable and high-performance web applications, (www.WebcodeforGeeks.com).

Advantages of PHP
Easy to Learn
PHP is one of the most user-friendly programming languages out there. This is due to the fact
that, despite its severe grammar, it is nevertheless forgiving. In addition to this obvious
advantage, you may use a variety of tools and resources to make sure your code is streamlined
for speed and effectiveness. PHP is a relatively easy language to learn compared to other
programming languages. Its syntax is similar to C, which makes it easier for developers to learn
if they have prior experience with C or other C-based languages, (www.WebcodeforGeeks.com).

Cross Platform
PHP can run on various operating systems like Windows, Linux, Unix, and macOS, making it a
cross-platform language. This allows developers to create applications that can run on different
types of servers and operating systems. Additionally, PHP is compatible with most web servers,
such as Apache and Nginx, making it easier to deploy applications, (www.GeeksforGeeks.org).

2|Page
Characteristics of PHP
Open Source
PHP is an open-source language because it was designed to be freely available and distributed
under the PHP License. PHP is an open-source server-side scripting language that is used to
develop static or dynamic websites or web applications.” The open-source nature of PHP allows
anyone to use, modify, and distribute the language as they see fit, without the need for
permission or payment. This has contributed to the widespread popularity of PHP as well as the
large community of developers and contributors who support its development and improvement.

Cross Platform
PHP can run on various operating systems like Windows, Linux, Unix, and macOS, making it a
cross-platform language. This allows developers to create applications that can run on different
types of servers and operating systems. Additionally, PHP is compatible with most web servers,
such as Apache and Nginx, making it easier to deploy applications, (TutorialsPoint, 2016).

Object Oriented Programming


PHP supports object-oriented programming (OOP), which makes it easier to write reusable and
maintainable code. OOP allows developers to organize code into classes, objects, and methods,
making it easier to manage large and complex applications. Additionally, OOP concepts like
inheritance and polymorphism can be used to reduce code duplication and improve code
readability, (TutorialsPoint, 2016).

Security
PHP has many pre-defined functions for data encryption. Users can also use third-party
applications for security. Security and flexibility are often contrasting features, but PHP
somehow manages to offer them both, and that’s great. PHP is designed specifically to be a more
secure language for writing CGI (Computer-generated Imagery) programs. Security algorithms
such as Sha1 (secure Hash algorithm 1) and MD5 (Message digest 5) are used to encrypt the
strings in PHP. Filter_var and strip_tags functions help to keep the environment more secure and
safe for users, (www.GeeksforGeeks.org).

3|Page
Where can PHP be applied?
PHP is a versatile language that can be applied in a variety of situations. The following are
examples of situations where PHP can be utilized, (TutorialsPoint, 2016):

Dynamic Web Pages


PHP can be used to build Content Management Systems like WordPress, Joomla and Drupal.
These systems are designed to allow different users create, manage and modify their own
websites without having to learn to write code. PHP is also used to create Blogs and Personal
Websites. PHP is an amazing language for the creation of dynamic content that allows easy
updates and content management, (TutorialsPoint, 2016).

User Authentication and Session management


PHP is used to manage the login information of different users. PHP can be utilized to design
user login systems on webpages, which are equipped with handling passwords and user
registration on a website. PHP is used in this regard because of the security it provides. PHP can
also be used to manage and restrict access of certain parts of a webpage. It also allows users
create sessions that can last for as long as they are logged in.

Prerequisites of PHP
Before you can begin writing code in PHP, it is necessary that you have basic knowledge in the
following languages: HTML, CSS and JavaScript (Optional).To write PHP code and run PHP on
your computer system, you need to have a reliable text editor as well as a PHP Server. Examples
of a text editor that can be used to write PHP are VS Code, Sublime text, Atom and many other
text editors that support PHP. Most of these text editors are available for download at no cost.

An example of a server that can be used when developing programs in PHP is Apache. This
server is available for free in a program called XAMPP. This program allows for a developer to
run PHP on their machine without having to employ a remote server, (TutorialsPoint, 2016).

General Syntax of PHP


PHP code can be written in a PHP file which can be linked to an HTML document, or it can be
embedded directly into HTML code. The following are examples of PHP syntax:

<?php … ?>

4|Page
The above tag is called a PHP tag, it is the most popular and most effective way to write PHP
code. The next example shows PHP code that is embedded within a HTML document.

<html>
<head> … </head>
<body>
<?php
echo “My First PHP script”;
?>
</body>
</html>

Variable Types in PHP


Variables are the main way to store information in a program. The following are some
characteristics of variables in PHP: All variables are declared with a dollar sign ($) at the
beginning; a variable can be overwritten, variables take the value of the most recent added value;
to assign a value to a variable an equal sign (=) is used; variables can be declared, but they work
even when declared upon use; Variables in PHP are case sensitive, meaning a variable must be
written exactly the way it was declared. The following are the different types of variables types
available in PHP, (www.GeeksforGeeks.org);

Integers: this is a variable type that holds numerical values. These values are whole numbers.
Integers do not have a decimal value, e.g. $int_var = 23. Integers can hold both positive and
negative numbers. The next type is a double, like integers doubles hold numerical values, with
the difference of doubles being able to hold decimal values, e.g. $double_var = 2.5.

The third type is the Boolean, the Boolean variable type can hold only two values which are true
or false, e.g. $bool_var = TRUE. The Boolean variable comes in handy when making decision
based on whether a condition is met. The fourth type is the NULL variable type, this is a special
type that can return just one value – NULL. A variable with a null value returns false when
tested with the IsSet() function. A variable can be declared null, like so; $my_var = NULL.

The fifth variable type is the String variable type. Strings are varibles that are able to store a
stram of characters. These characters could be names, sentences and paragraphs. An example of
a string variable is; $sentence = “This is 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

5|Page
the string to be printed. When single quotes are used, the value of the variable will not be printed
rather the text with the variable name will be displayed, (TutorialsPoint, 2016).

Operator Types in PHP


The PHP language supports a variety of operators which include; arithmetic operators,
comparison operators, Logical Operators, Assignment operators and conditional operators.

Arithmetic Operators
The following are the arithmetic operators that are supported in PHP. Addition (+), this operator
adds two operands; Subtraction (-) subtracts two operands; Multiplication (*) multiplies two
operands; Division (/) divides two operands; Modulus (%), returns the remainder after dividing
two values; Exponentiation (**) raises a value to the given power; Increment Operator (++)
increases a value by 1; Decrement operator (--) decreases a value by 1,
(www.GeeksforGeeks.org).

Logical Operators
Logical operators in PHP return a True or false value after evaluating. PHP supports the
following logical operators: And operator (&&), this operator returns true only if both values are
true. If any of the values is false, the And operator will return false. The next operator is the Or
operator (| |), this operator evaluates to True if either or both of the values is true. The next
operator is the Not operator (!), which changes a value from true to false and from false to true.

Comparison Operators
Comparison operators are designed to make a comparison between two values to evaluate their
similarities. The following are some of the comparison operators that are supported in the PHP
language. Equal to (= =), the = sign is not used to compare two values in PHP, it is an
assignment operator, but when comparing whether two values are the same, two equal signs are
used to show equal to. The second comparison operator is the identical operator (= = =), the
identical works similar to the equal to operator, with the difference that this operator checks
whether the two values being compared also have the same variable type,
(www.GeeksforGeeks.org).

The next comparison operators are the greater than (>) and less than (<) operators. The greater
than operator compares two values and checks whether the value on the left side of the operator

6|Page
is bigger than the value on the right side of the operator. This operator will evaluate to true if the
number on the left is bigger than the number on the right. The less than operator works similar
but opposite to the greater than operator, (TutorialsPoint, 2016). When evaluating two values it
checks whether the value on the left is smaller than the value on the right.

Decision Making in PHP


When code is written, there comes a point when a certain decision has to be made based on the
choice made by a user, or on the result of preset actions. This is called branching,
(www.GeeksforGeeks.org). The following are the Branching statements that are supported in
PHP:

If Statement
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
$age = 16;
If($age >= 16){
echo “You can get your NRC”;
}
?>
If … else statement
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
$age = 15;
If($age >= 16){
echo “You can get your NRC”;
} else {
Echo “You are not old enough to get registered”;
}
?>
Switch Statement
The switch statement makes a decision from evaluating a series of conditions. It can switch from
one condition to another until it find a true statement. The break key word is important in the
switch statement because it tells the programs to stop executing once a true statement is found.
The default statement is evaluated if none of the values are true. The following example,
illustrates the switch statement, (www.W3Schools.com).

7|Page
<?php
$grade = 8;
switch ($grade){
case 8:
echo “Welcome to secondary school!”;
break;
case 9:
echo “Are you ready for the exams!”;
break;
case 10:
echo “Welcome to senior secondary school!”;
break;
case 11:
echo “You are almost there!”;
break;
case 12:
echo “You are at the finish line!”;
break;
default:
echo “You are currently not in secondary school”;
}
?>

PHP Loops
Loops in programming are used to execute code that needs to be repeated for a set number of
times, or until a certain condition has been met. The following are the loops that are supported in
PHP:
While Loop
This loop is designed to continues executing 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 = 1;
while ($x <=5) {
echo “The value of x 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, .
<?php
$x = 1;
do {
echo “The value of x is: $x;”;
$x++;
} while ($x <= 5);

8|Page
?>
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 “The value of x 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, (www.GeeksforGeeks.org).
<?php
$colors = array(“black”, “orange”, “green” , “white”);
foreach($colors as $value){
echo “$value \n”;
}
?>

GET and POST Methods


The GET and POST methods are methods that are used in PHP to send data from a browser to a
server. The data is first collected from forms that are available to the user and then it is
transferred to the server where it is stored for further processing.

Characteristics of the GET method include; Data is appended to the URL, when using this
method data is attached to the URL in the form. Since data is sent in the URL it means the GET
method has a limited size. The GET method is not very secure, because data is sent in the URL it
makes it unsuitable for sending data such as usernames, passwords, accounts data and other
pieces of sensitive information. To access data sent using the GET method in PHP, we use the
superglobal $_GET, (www.GeeksforGeeks.org).

The POST method unlike the GET method does not transmit data through the URL. This
characteristic of the POST method makes it best suited to transmit sensitive data. Instead of
transmitting data through the URL the data is sent through the body of the HTTP request. The
POST method not transmitting data through the URL means it is not limited in size like the GET
method. Thus, large amounts of data can be transmitted through this method. Data sent through
the POST method cans be accessed through the $_POST superglobal.

What are Cookies?


Cookies in PHP are small pieces of data that a server sends to user’s web browser. The browser
saves cookies and sends theme to the server with subsequent requests. Cookies make it easy for

9|Page
the server to remember specific information about the user making a request. Cookies are mainly
used when managing sessions, storing preferences such as theme, and tracking the activity of a
user on the web, (Page, 2018)

REFERENCES

Page, B. (2018). The Ultimate Guide to PHP Programming.


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/
(www.WebCodeGeeks.com). PHP Programming Cookbook

10 | P a g e

You might also like