PHP: Hypertext Preprocessor - Yasham Academy

PHP: Hypertext Preprocessor

by | Dec 27, 2022 | Blogs | 0 comments

What is PHP?

PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML .

PHP is a genaral-purpose scripting language  geared toward web development. It was originally created by Danish-Canadian programmer Rasmus Lerdof  in 1993 and released in 1995. The PHP reference implementation  is now produced by The PHP Group.PHP originally stood for Personal Home Page, but it now stands for the recursive initialism.

PHP can be easily embedded with HTML files and HTML codes can also be written in a PHP file. The thing that differentiates PHP and HTML is, PHP codes are executed on the server whereas HTML codes are directly rendered on the browser. PHP codes are first executed on the server and then the result is returned to the browser. The only information that the client or browser knows is the result returned after executing the PHP script on the server and not the actual PHP codes present in the PHP file. Also, PHP files can support other client-side scripting languages like CSS and JavaScript.

What Can PHP Do?

  • PHP can generate dynamic page content.
  • PHP can create, open, read, write, delete, and close files on the server.
  • PHP can collect form data.
  • PHP can send and receive cookies.
  • PHP can add, delete, modify data in your database.
  • PHP can be used to control user-access.
  • PHP can encrypt data.

 

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 free. Download it.
  • PHP is easy to learn and runs efficiently on the server side.
  • PHP 8.1;- PHP 8.1 is the latest version of PHP, a popular programming language used for web development and server-side scripting. It was released on December 8, 2021 and includes a number of new features and improvements over previous versions. Here is a more detailed look at some of the key features and improvements in PHP 8.1:
  • Union types:-This feature allows you to specify that a variable can have multiple types, such as an integer or a string. This can be useful for writing more flexible and robust code, as it allows you to handle a wider range of input values.
  • Nullsafe operator:- This operator allows you to chain method calls without worrying about null references. For example, if you want to call a method on an object that may be null, you can use the nullsafe operator to ensure that the method is only called if the object is not null.
  • Match expression: This is a more concise and flexible way to perform matching on values. It allows you to specify multiple cases and their associated values, and then use the match keyword to select the appropriate case based on the value of an expression.
  • Improved performance: PHP 8.1 includes various performance improvements, such as faster array operations and better memory usage. These improvements can help make your code run faster and more efficiently.
  • New error handling features: PHP 8.1 introduces a new throws keyword that allows you to specify which exceptions a function can throw, and a new assert() function that can be used to check for certain conditions at runtime. These features can help you write more reliable and robust code.

Common uses of PHP

  • PHP performs system functions, i.e. from files on a system it can create, open, read, write, and close them.
  • PHP can handle forms, i.e. gather data from files, save data to a file, through email you can send data, return data to the user.
  • You add, delete, modify elements within your database through PHP.
  • Access cookies variables and set cookies.
  • Using PHP, you can restrict users to access some pages of your website.
  • It can encrypt data.

 

 Features of PHP

 

  • PHP performs system functions, i.e. from files on a system it can create, open, read, write, and close them.
  • PHP can handle forms, i.e. gather data from files, save data to a file, through email you can send data, return data to the user.
  • You add, delete, modify elements within your database through PHP.
  • Access cookies variables and set cookies.
  • Using PHP, you can restrict users to access some pages of your website.
  • It can encrypt data.
What are the features that make PHP efficient?

Great session management, eliminating unnecessary memory allocation, are some of the features that make PHP efficient. PHP scrips are usually faster than other scripting languages. Users can load their web pages faster, and they love it. PHP code runs faster than most of programming languages because it runs in its own memory space.
 

Characteristics of PHP Variables

Here are the most important things to know about variables in PHP.

  • All variables in PHP are denoted with a leading dollar sign ($).
  • The value of a variable is the value of its most recent assignment.
  • Variables are assigned with the = operator, with the variable on the left-hand side and the expression to be evaluated on the right.
  • Variables can, but do not need, to be declared before assignment.
  • Variables in PHP do not have intrinsic types – a variable does not know in advance whether it will be used to store a number or a string of characters.
  • Variables used before they are assigned have default values.
  • PHP does a good job of automatically converting types from one to another when necessary.
  • PHP variables are Perl-like.

Php Data Types

  • Integers − are whole numbers, without a decimal point, like 4195.
  • Doubles − are floating-point numbers, like 3.14159 or 49.1.
  • Booleans − have only two possible values either true or false.
  • NULL − is a special type that only has one value: NULL.
  • Strings − are sequences of characters, like ‘PHP supports string operations.’
  • Arrays − are named and indexed collections of other values.
  • 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).

PHP Syntax

A PHP script starts with <?php and ends with ?>

<?php
// PHP code goes here
?>

File extension for PHP files is “.php“.

Note: PHP statements end with a semicolon (;).

PHP Case Sensitivity

In PHP, keywords (e.g. ifelsewhileecho, etc.), classes, functions, and user-defined functions are not case-sensitive.

But all variable names are case-sensitive.

PHP Echo and Print Statements

 

echo and print are  same. They are both used to output data to the screen.

 

echo has no return value while print has a return value of 1 so it can be used in expressions. echo can take multiple parameters.  while print can take one argument. echo is faster than print.

PHP Operators

Operators are used to perform operations on variables and values.

PHP divides the operators in the following groups:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Increment/Decrement operators
  • Logical operators
  • String operators
  • Array operators
  • Conditional assignment operators

PHP Arithmetic Operators

 

Operator Name Example Result
+
Addition
$x + $y Sum of $x and $y
Subtract $x *$y Difference of $x and $y
* Multiply $x * $y Product of $x and $y
/ Division $x /$y
Quotient of $x and $y
% Modulus $x * $y Remainder of $x divided by $y

PHP Comparison Operators

The PHP comparison operators are used to compare two values

 

Operator Name Example Result
== Equal $x == $y Returns true if $x is equal to $y
=== Identical $x === $y Returns true if $x is equal to $y, and they are of the same type
!= Not equal $x != $y Returns true if $x is not equal to $y
<> Not equal $x <> $y Returns true if $x is not equal to $y
!== Not identical $x !== $y Returns true if $x is not equal to $y, or they are not of the same type
> Greater than $x > $y Returns true if $x is greater than $y
< Less than $x < $y Returns true if $x is less than $y
>= 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

Assert() Error handling:-

The assert() function and the throws keyword are new error handling features in PHP 8.1 that can help you write more reliable and robust code.

The assert() function is used to check for certain conditions at runtime. If the condition is not met, the function generates an AssertionError exception

 

assert($m > 1, '$m must be greater than one');

 

In this example, the assert() function checks whether $x is greater than zero. If it is not, an AssertionError exception is thrown with the message “$x must be greater than zero“. The throws keyword, on the other hand, is used to specify which exceptions a function or method can throw. This can be helpful for documenting your code and making it easier for other developers to understand how your functions and methods behave. For example:

function divide(int $x, int $y): int
{
    if ($y === 0) {
        throw new DivisionByZeroError('Cannot divide by zero');
    }

    return $x / $y;
}

In this example, the divide() function uses the throws keyword to specify that it can throw a DivisionByZeroError exception if the second argument is zero.

Overall, the assert() function and the throws keyword are useful tools for handling errors and exceptions in PHP, and can help you write more reliable and robust code.