Swapnil Banga | 01 Nov, 2023

What is PHP? Learn All About the Scripting Language

 

What is PHP, and what is PHP used for exactly? PHP is a server-side scripting programming language that’s used to develop static or dynamic websites, or even web applications. 

What does PHP stand for? PHP stands for Hypertext Preprocessor, but you may recognize it as the old acronym for “Personal Home Pages.” 

A script is a set of programming instructions interpreted at runtime. A scripting language is responsible for interpreting scripts at runtime. The primary objective of a script is to escalate the performance of various tasks in an application. 

But what is PHP programming, and how can you use it? We’ll cover all that today — read on to gather an introduction to PHP and learn all about its uses in the computer world. 

What is PHP Programming?

PHP stands for hypertext preprocessor. The PHP programming language is a scripting language used widely across the globe, primarily for web development. You can embed PHP into HTML and work after that too.

Moreover, PHP is server-side, whereas other programming languages like JavaScript are client-side. The major difference between the two is that PHP code is executed on the server that generates HTML, and is sent directly to the client. While the client can see the results by running the script, they cannot access the underlying code. 

If you are a beginner, PHP is an extremely simple language to learn., with lots of excellent PHP courses to choose from.

It also comprises advanced features for professional programmers. You can start by writing simple scripts and master the PHP programming language gradually. 

Now, let’s dive into the syntax of PHP.

Syntax of PHP

PHP syntax is pretty simple. A PHP file can also include HTML or client-side script as JavaScript. 

Here is the basic syntax of PHP:

<?php

echo ‘I love programming’;

?>

The display screen shows, “I love programming.” 

When using PHP, keep the following in mind:

  • While not essential, some prior understanding of HTML is ideal before learning the PHP programming language. 
  • To handle database-powered applications with PHP, use database management systems or DBMS.
  • You will need XML or JavaScript to build advanced interactive applications with PHP.

How to Write a Program in PHP

Writing a program in PHP is extremely easy. See the below example:

<!DOCTYPE html>

<html>

 <head>

 <title>Example</title>

 </head>

 <body>




 <?php

 echo "PHP Programming is easy.";

 ?>




 </body>

</html>

Let’s move ahead and know why to use PHP. 

Why PHP?

As you know, there are a plethora of programming languages available for you to work with as a web developer. So, what makes PHP stand out from the others? Here are a few benefits of using PHP: 

  • PHP is a free and open-source scripting language.
  • PHP is a server-side scripting language. You can install it on the server; however, the clients who request the resources from the server don’t have to install PHP.
  • It is cross-platform, allowing you to use it on various operating systems like Windows, macOS, Linux, etc.
  • PHP comes with a built-in support system, which means that you can use PHP with various database management systems such as Oracle, Postgres, ODBC, MS, and SQL Server.
  • Most web hosting servers support PHP.
  • PHP provides you with a huge online community with all kinds of support, including documentation guides and problem-solving posts. 
  • PHP is easy to learn, especially if you have a basic knowledge of other programming languages.
  • Vast text-processing features like PCRE and Perl

In addition, PHP offers a remarkable feature by supporting a wide range of databases. For example, you can use database-specific extensions like MySQL to write a database-enabled web page, use an abstraction layer such as PDO, or even build a connection to a database with the ODBC extension. 

Furthermore, while using PHP, you can use both object-oriented programming and procedural programming. You are not just bound to output HTML; rather, you can output any text like XML or XHTML. PHP can auto-generate search files and save them, helping to protect the server-side from forming a cache for the dynamic content. 

Now that we know some of PHP’s benefits, let’s get into the PHP extensions. 

PHP Extensions

PHP files are saved with the “.php” extension. Some of the earlier PHP file extensions include:

  • .phps
  • .php3
  • .php4
  • .php5
  • .phtml

Where is PHP Used?

There are various fields where you can use PHP, and but these are the three most vital areas to use it:

  • Command-line scripting
  • Server-side scripting
  • Writing desktop applications

Command-Line Scripting

Several tasks run in the background of a web server besides web applications for the databases. Each task concludes after a designated amount of time. Therefore, every task has its timeline.

Here’s an example of command-line scripting in PHP:

Suppose you have to send an invitation email to your subscribers on a mailing list. You can carry out this task via web scripting. However, the process gets trickier when you reach a few hundred subscribers. And, if there are thousands of subscribers, a web script will only have a couple of minutes for execution, a period known as maximum execution time. At the end of this period, the web server terminates the web script and fails to send the email to a portion of the subscribers. 

PHP command-line scripting can resolve this issue:

With command-line scripts, there is no maximum execution time. The scripts can run as long as the server is active. 

Hence, with the help of command-line scripting, any kind of time-consuming tasks such as transferring files to another server via FTP or backing up a complete website or database can be done seamlessly.

Server-Side Scripting

Server-side scripting is the most common and frequently used field for PHP. It requires three vital components: a webserver, a web browser, and a PHP parser. 

With server-side scripting, you must run the web server with installed PHP. Moreover, you can easily access and run any PHP program output through a web browser. 

Writing Desktop Applications

If you want to develop a desktop application and a GUI, PHP may not be the perfect solution for you. However, to access some advanced PHP features, you can use PHP-GTK in your client-side applications to write programs. 

PHP-GTK is an extension of PHP. However, you may not find it in the main distribution. To access PHP-GTK, visit the official website

PHP Data Types and Operators

PHP Data Types Diagram

PHP supports a wide set of data types, such as:

  • Integer
  • String
  • Float
  • Array 
  • Boolean

PHP Integer

A non-decimal number within the range of -2,147,483,648 and 2,147,483,647 is an integer data type. A number is an integer only when at least one digit is either positive or negative. Take a look at the below command that takes $z as an integer, with the function var_dump returning the value:

<?php 

$z =1674; var_dump($z); 

php?>

PHP String

A PHP string is any sequence of characters. Here’s the syntax to use a string in PHP:

<?php 

$a = “Hello Programming!”; 

$b = ‘Hello Programming!’; 

echo $a; echo “<br>”; 

echo $b; 

?>

PHP Float

A floating-point number is any number with a decimal point or a number that's present in an exponential form. 

Here is an example:

<?php 

$a =3.14;

var_dump($z);

?>

PHP Array

An array holds multiple values in one variable:

<?php 

$students = array(“Daniel”,”Josh”,”Sam”);

var_dump($students); 

?>

PHP Boolean

A Boolean is used for conditional testing, and it tells you two states:

  • True
  • False

$x = true;

$y = false;

Now, let's move on to the PHP operators.

PHP Operators

PHP Operators Diagram

Operators are primarily used to perform various operations on variables. Different operators in PHP include:

  • Logical operators
  • Arithmetic operators
  • Array operators 
  • Assignment operators
  • Comparison operators

Logical Operators

Logical operators help to combine conditional statements:

Name of the operator

Sign

And

&

Or

|

Xor

xor

Or

||

And

&&

Not

!

 

Arithmetic Operators

Arithmetic operators like additional, subtraction, multiplication and division help perform operations with numeric values. 

Name of the operator

Sign

Example

Addition

+

$x + $y

Subtraction

-

$x - $y

Division

/

$x / $y

Multiplication

*

$x * $y

Exponentiation

**

$x ** $y

Modulus

%

$x % $y

 

Array Operators

PHP arrays are used to compare arrays:

Name of the operator

Sign

Union

+

Equality

==

Inequality

!=

Identity

===

Non-identity

!==

Inequality

<>

 

Assignment Operators

Assignment operators assign a value to a particular variable: 

Operator

Meaning

x = y

x = y

x += y

x = x+y

x -=y

x = x-y

 

Comparison Operators

To compare two numbers or strings, we use comparison operators:

Name of the Operator

Sign

==

Equal

===

Identical

!==

Not identical

!=

Not equal

<>

Not equal

>

Greater than

<

Less than

<=

Less than or equal to

>=

Greater than or equal to

 

PHP Cookies and Sessions

Cookies

We use cookies to identify users on a webpage. A computer sends a request to a page in the browser, and with it, sends a cookie. In the case of PHP, you can easily retrieve and create cookie values. 

Here are some commands to create or modify a cookie:

To create a cookie, follow this code:

<?php $cookie_name = "user"; 

$cookie_value = "PhP Programming"; 

setcookie($cookie_name, $cookie_value, time() + (60*60), "/"); 

?> 

<html> 

<body> 

<?php

 

if(!isset($_COOKIE[$cookie_name])) 

{ echo "Cookie named '" . $cookie_name . "' is not set!"; } 

else { echo "Cookie '" . $cookie_name . "' is set!

<br>"; 

echo "Value is: " . $_COOKIE[$cookie_name]; } 

?> 

</body> 

</html>

In this code, the syntax is:

setcookie(name, value, expire, path, domain, secure, httponly);

To modify a cookie, follow this code:

<?php 

$cookie_name = "user"; 

$cookie_value = "XYZ"; 

setcookie($cookie_name, $cookie_value, time() + (60*60), "/"); 

?> 

<html> 

<body> 

<?php 

if(!isset($_COOKIE[$cookie_name])) 

{ echo "Cookie named '" . $cookie_name . "' is not set!"; } 

else { echo "Cookie '" . $cookie_name . "' is set!

<br>"; 

echo "Value is: " . $_COOKIE[$cookie_name]; } 

?> 

</body> 

</html>

To delete a cookie, use this code:

<?php 

setcookie("user", "", time() - 86400); 

?> 

<html> 

<body> 

<?php 

echo "Cookie 'user' is deleted."; 

?> 

</body> 

</html>

In the example above, setcookie here sets the expiration time to be a day ago.

PHP Sessions

You can store information with multiple pages by using a PHP session, without storing the information in the user’s system.

To create a session, follow this code:

?php 

session_start(); 

?> 

<!DOCTYPE html>

 <html> 

<body> 

<?php 




$_SESSION["favfont"] = "Arial"; 

$_SESSION["favbird"] = "vulture"; 

echo "Session variables are set."; 

?> 

</body>

 </html>

To modify a session, you can execute:

<?php 

session_start(); 

?> 

<!DOCTYPE html> 

<html> 

<body> 

<?php 

$_SESSION["favcolor"] = "yellow"; 

print_r($_SESSION); 

?> 

</body> 

</html>

Lastly, write the following code to delete a session:

<?php 

session_start(); 

?> 

<!DOCTYPE html> 

<html> 

<body> 

<?php 

session_unset(); 

session_destroy(); 

?> 

</body> 

</html>

Conclusion

What is PHP? Standing for Hypertext preprocessor, PHP is an open-source scripting language popularly used in the web development world. PHP is primarily based on server-side scripting; however, there is much more to PHP, including its cookies and extensions, benefits, and open-source nature. 

Now that you have a basic PHP introduction, it’s time to apply your knowledge to PHP projects.

Download 10 best PHP projects with source code to get some hands-on experience with using PHP. 

Check This Course Out! PHP for Absolute Beginners

PHP for Absolute Beginners

 

By Swapnil Banga

Software engineer, hardware enthusiast, writer by avocation and a gamer. Swapnil has been working on Hackr for a large part of his career. Primarily working on Laravel, he is also the author of our React Native Android app. When not in front of a screen, you will find him devouring a novel or listening to heavy metal.

View all post by the author

Subscribe to our Newsletter for Articles, News, & Jobs.

Thanks for subscribing to the hackr.io newsletter!

Disclosure: Hackr.io is supported by its audience. When you purchase through links on our site, we may earn an affiliate commission.

In this article

Learn More

Please login to leave comments