PHP is HyperText Preprocessor. It is an open source programming language which is used for server side web development.
● The key feature of PHP is that it supports multiple databases like mysql, oracle, sybase, postgreSQL etc.
● Also PHP is an object oriented language.
● It can be easily embedded in HTML and scripts.
Some of the popular and commonly used PHP frameworks are:
● CakePHP : It is an open source web framework written in PHP. It follows the model-view-controller concept, and distributed under MIT licence.
Its repository is on GitHub : https://github.com/cakephp/cakephp.
● CodeIgniter : It is an open source web framework written in PHP. It follows the model-view-controller concept, and distributed under MIT licence. While controller classes are a necessary part of development under CodeIgniter, models and views are optional.
Its repository is on GitHub : https://github.com/bcit-ci/CodeIgniter.
● Symfony : It is an open source web framework written in PHP. It is distributed under MIT licence. It is a set of reusable PHP components/libraries. Symfony has a low performance overhead used with a bytecode cache.
Its repository is on GitHub : https://github.com/symfony/symfony.
● Zend Framework : It is an open source web framework written in PHP 7. It is distributed under BSD licence. The framework uses various packages by the use of Composer as part of its package dependency managers.
Its repository is on GitHub : https://github.com/zendframework/zendframework.
Echo is used to output one or more strings in PHP.
● Echo don't have return value.
● We can pass multiple strings in echo which can be comma separated.
● Echo is faster then print statement.
Here is sample code snippet of echo():
Sample 1:
Following are the key differences between echo and print:
● Echo don't return any value whereas print do. And print always return value 1.
● Echo is faster whereas print is slow.
● In echo we can pass multiple strings separated by commas whereas in print we cant pass multiple strings.
The reason why echo is faster than print is that it doesn't return any value.
GET method can transfer max of 1024 bytes whereas POST can send large chunks of data.
POST method is much secure then GET method.
A variable in PHP is the temporary storage location which holds data. We don't need to declare the variable explicitly as string, integer or float.
The values can be directly assigned to the variables.
$s="hello";
$x=10;
$y=4.6;
The variable name should be start with letter or underscore. It cant start with the numbers or special characters.
Variables can be added without declaring there type.
In PHP following are the ways to define the variables as constant:
● Define function e.g. define("variable","constant val");
● Const keyword can be use with the variable name e.g. const $a = 10;
Magic constants are the pre-defined constants in PHP which starts and ends with double underscore. These are case-insensitive. Some of these are __LINE__, __FILE__, __DIR__, __CLASS__, __FUNCTION__ etc.
In PHP following are the data types:
● Scalar data types - integer, boolean, string, float
● Compound data types - array, object
● Special data types - resource, NULL
Similar to other programming languages, PHP also has arrays, these are multiple values of same type.
Following are the different type of arrays:
● Indexed Array: There index is represented via a $a[0] = 1
● Associative Array: Associate name with each array element e.g. $sal["Son"]="3500";
● Multidimensional Array: These are the arrays of array e.g. $emp[$r][$c]
Following are the key functions of string in PHP:
● strtolower() : This function convers the string to lowecase.
● strtoupper() : This function convers the string to uppercase.
● ucfirst() : This function converts the first character of a string to uppercase.
● lcfirst() : This function converts the first character of a string to lowercase
● ucwords(): This function converts the first character of each word in a string to uppercase.
● strrev() : This function reverses a string.
● strlen() : This function returns the length of a string. Here is the sample code snippet:
There are two ways to include files in PHP:
● include
● require
● The biggest advantage of including files is the re-usability of the code.
● Upon failure require produces a fatal error (E_COMPILE_ERROR) and stop the script.
● Whereas upon failure include only produce a warning (E_WARNING) and the script will continue.
● Including files saves a lot of work. This means that you can create a standard header, footer, or menu file for all your web pages. Then, when the header needs to be updated, you can only update the header include file.
● Here is the syntax of both the types:
include 'filename'; or require 'filename';
If file is not found then include will send warning whereas require send fatal error.
Session in used to temporary store the data so that it can be transferred across different PHP pages.
Session_start() function is used to start or resume the session.
session_start();
$_SESSION[""tmp""] = ""Scale"";
echo $_SESSION[""tmp""];
It is good practice to destroy all the sessions after using them in the program. This can be done by using below mentioned function:
session_destroy();
This function is used to set cookie in PHP which can then be accessed globally by using $_COOKIE. setcookie(""name"", ""abc""); $_COOKIE[""name""];
In PHP, $var is the variable that stores value like float, integer, string etc. whereas $$var is the reference variable that stores the value of other variable.
These are the functions which allows to define default value such that if no argument value passes then it will take default value.
eg function add($x=2){}
These are the functions in PHP which allows to pass n number of arguments in function. Here is the sample example:
function add(...$x){}
In PHP we can do following operations in file - read, write, append, delete, create and close file. Fopen() - To open file in different modes like read-only, write-only, read-write etc. fclose() - To close file fread() - To read file fwrite() - To write onto the file unlink() - This is used to delete file
This is global file contain all the file information such as type, size and error association.
$_FILES['filename']['name']
$_FILES['filename']['type']
mysqli_connect() function is used to connect to MYSQL database. Below is the sytanx to connect: mysqli_connect (server, username, password)
mysqli_close() function is used to disconnect from MYSQL database.
mysqli_query($conn,$sql) function is used to execute SQL query. Here $conn is the connection object that we have got in return of mysqli_connect(). $sql is the SQL query that you want to execute.
This function is used to check if there is any value exits in array.
Explode function breaks the string into an array. Here is the syntax:
explode ( string $delimiter , string $string [, int $limit ] );
Implode function joins the elements of array and returns string. Here is the syntax:
implode(separator,array)
In PHP filters are used to validate and filter the data.
Few of the predefined filter constants are:
FILTER_VALIDATE_BOOLEAN - Validates boolean
FILTER_VALIDATE_EMAIL - Validates email address
FILTER_VALIDATE_URL - Validates URL
This is predefined filter constant which is being used to remove all the illegal characters from the email address.
Example:
$email = "[email protected]//.com";
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
The output is : [email protected]
This is predefined filter constant which is being used to remove the special characters.
This is predefined filter constant which is being used to validate an IP address. This enables to detect the valid or invalid IP addresses.
Die() function is used to handle the error if something goes wrong in the code. Example:
if(! $retval ) { die('Could not get data: ' . mysqli_error()); }
The file where we define RewriteRule is .htaccess.
Here is the code snippet which need to define in .htaccess to rewrite the url for html pages:
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^\.]+)$ $1.html [NC,L]