PHP stands for Hypertext Preprocessor. PHP is an interpreted language, i.e., there is no need for compilation. PHP is a server-side scripting language. PHP is faster than other scripting languages, for example, ASP and JSP.
In this tutorial, you will get a lot of PHP examples to understand the topic well. You must save the PHP file with a .php extension. Let's see a simple PHP example.
File: hello.php Hello by PHP"; ?>
Output: Hello by PHP
PHP means PHP: Hypertext Preprocessor.
The function get_magic_quotes_gpc() tells us whether the magic quotes is switched on or no.
The strip_tags() function enables us to clean a string from the HTML tags.
The old name of PHP was Personal Home Page.
In static websites, content can't be changed after running the script. You can't change anything on the site. It is predefined. In dynamic websites, content of script can be changed at the run time. Its content is regenerated every time a user visit or reload. Google, yahoo and every search engine is the example of dynamic website.
The scripting engine that powers PHP is called Zend Engine 2.
PHP4 doesn't support oops concept and uses Zend Engine 1. PHP5 supports oops concept and uses Zend Engine 2.
WordPress: 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: 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: 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. Magento utilizes E-trade stage which offers organization extreme E-business arrangements and extensive support network. Drupal: Drupal is a CMS platform developed in PHP and distributed under the GNU (General Public License).
CakePHP CodeIgniter Yii 2 Symfony Zend Framework etc.
PHP has borrowed its syntax from Perl and C.
Scalar type declarations Return type declarations Null coalescing operator (??) Spaceship operator Constant arrays using define() Anonymous classes Closure::call method Group use declaration Generator return expressions Generator delegation Space ship operator
PHP echo output one or more string. It is a language construct not a function. So the use of parentheses is not required. But if you want to pass more than one parameter to echo, the use of parentheses is required.
Syntax: void echo ( string $arg1 [, string $... ] )
PHP print output a string. It is a language construct not a function. So the use of parentheses is not required with the argument list. Unlike echo, it always returns 1.
Syntax: int print ( string $arg)
Echo can output one or more string but print can only output one string and always returns 1. Echo is faster than print because it does not return any value.
A PHP variable is the name of the memory location that holds data. It is temporary storage.
$message stores variable data while $$message is used to store variable of variables. $message stores fixed data whereas the data stored in $$message may be changed dynamically.
PHP constants are name or identifier that can't be changed during execution of the script. PHP constants are defined in two ways:
Using define() function Using const() function
PHP magic constants are predefined constants, which change based on their use. They start with a double underscore (__) and end with a double underscore (__).
PHP data types are used to hold different types of data or values. There are 8 primitive data types which are further categorized in 3 types:
Scalar types Compound types Special types
PHP single line comment is made in two ways:
Using // (C++ style single line comment) Using # (Unix Shell style single line comment)
PHP multi-line comment is made by enclosing all lines within.
For, while, do-while and for each.
PEAR means "PHP Extension and Application Repository". It extends PHP and provides a higher level of programming for web developers.
Version 7.1 or 7.2 is the recommended version of PHP.
Just use the PHP command line interface (CLI) and specify the file name of the script to be executed as follows:
php script.php
Just use the PHP CLI program with the option -a as follows:
php -a
The two most common ways to start and finish a PHP script are:
To be able to display the output directly to the browser, we have to use the special tags .
PHP 5 presents many additional OOP (Object Oriented Programming) features.
PHP supports only single inheritance; it means that a class can be extended from only one single class using the keyword 'extended'.
'final' is introduced in PHP5. Final class means that this class cannot be extended and a final method cannot be overridden.
We use the operator '==' to test is two objects are instanced from the same class and have same attributes and equal values. We can test if two objects are referring to the same instance of the same class by the use of the identity operator '==='.
It is possible to generate HTML through PHP scripts, and it is possible to pass pieces of information from HTML to PHP.
If we would like to pass values through a form or an URL, then we need to encode and to decode them using htmlspecialchars() and urlencode().
PHP and Javascript cannot directly interact since PHP is a server side language and Javascript is a client-side language. However, we can exchange variables since PHP can generate Javascript code to be executed by the browser and it is possible to pass specific variables back to PHP via the URL.
GD library is needed to execute image functions.
imagetypes() gives the image format and types supported by the current version of GD-PHP.
The functions are getimagesize() for size, imagesx() for width and imagesy() for height.
If the function require() cannot access the file then it ends with a fatal error. However, the include() function gives a warning, and the PHP script continues to execute.
require(), and require_once() perform the same task except that the second function checks if the PHP script is already included or not before executing it. (same for include_once() and include())
To be able to display a human-readable result we use print_r().
The set_time_limit(0) added at the beginning of a script sets to infinite the time of execution to not have the PHP error 'maximum execution time exceeded.' It is also possible to specify this in the php.ini file.
This is a PHP syntax error expressing that a mistake at the line x stops parsing and executing the program.
The most common and used way is to get data into a format supported by Excel. For example, it is possible to write a .csv file, to choose for example comma as a separator between fields and then to open the file with Excel.
file_get_contents() lets reading a file and storing it in a string variable.
To be able to connect to a MySQL database, we must use mysqli_connect() function as follows:
mysql_pconnect() ensure a persistent connection to the database, it means that the connection does not close when the PHP script ends. This function is not supported in PHP 7.0 and above
The result set can be handled using mysqli_fetch_array, mysqli_fetch_assoc, mysqli_fetch_object or mysqli_fetch_row.
The function mysqli_num_rows() returns the number of rows in a result set.
mysqli_affected_rows() return the number of entries affected by an SQL query.
The mysqli_fetch_object() function collects the first single matching record where mysqli_fetch_array() collects all matching records from the table in an array.
To access the data sent via the GET method, we use $_GET array like this: $variable = $_GET["var"]; this will now contain 'value'
To access the data sent this way, you use the $_POST array. Imagine you have a form field called 'var' on the form when the user clicks submit to the post form, you can then access the value like this:
It is possible to use the dedicated function, is_numeric() to check whether it is a number or not.
It is possible to use the dedicated function, ctype_alnum to check whether it is an alphanumeric value or not.
If we want to check whether a variable has a value or not, it is possible to use the empty() function.
The unlink() function is dedicated for file system handling. It simply deletes the file given as entry.
The unset() function is dedicated for variable management. It will make a variable undefined.
The addslashes function enables us to escape data before storage into the database.
The stripslashes function enables us to remove the escape characters before apostrophes in a string.
We have to enable the Magic quotes entry in the configuration file of PHP.
PHP fwrite() and fputs() functions are used to write data into file. To write data into a file, you need to use w, r+, w+, x, x+, c or c+ mode.
The unlink() function is used to delete a file in PHP.
You should just run the PHP command line interface (CLI) and specify the file name of the script to be executed as follows.
The move_uploaded_file() function is used to upload file in PHP.
bool move_uploaded_file ( string $filename , string $destination )
The readfile() function is used to download the file in PHP.
int readfile ( string $filename )
The mail() function is used to send email in PHP.
bool mail($to,$subject,$message,$header);
There are two methods to connect MySQL database with PHP. Procedural and object-oriented style.
The mysqli_connect() function is used to create a connection in PHP.
resource mysqli_connect (server, username, password)
Since PHP 4.3, mysql_reate_db() is deprecated. Now you can use the following 2 alternatives.
mysqli_query() PDO::_query()
By default, the maximum execution time for PHP scripts is set to 30 seconds. If a script takes more than 30 seconds, PHP stops the script and returns an error. You can change the script run time by changing the max_execution_time directive in the php.ini file. When a script is called, set_time_limit function restarts the timeout counter from zero. It means, if default timer is set to 30 sec, and 20 sec is specified in function set_time_limit(), then script will run for 45 seconds. If 0sec is specified in this function, script takes unlimited time.
There are 3 types of error in PHP.
Notices:These are non-critical errors. These errors are not displayed to the users. Warnings:These are more serious errors, but they do not result in script termination. By default, these errors are displayed to the user. Fatal Errors:These a
The exit() function is used to stop the execution of PHP script.
The .htaccess is a configuration file on Apache server. You can change configuration settings using directives in Apache configuration files like .htaccess and httpd.conf.
The PHP explode() function breaks a string into an array.
The PHP split() function splits string into an array by regular expression.
A persistent cookie is permanently stored in a cookie file on the browser's computer. By default, cookies are temporary and are erased if we close the browser.
imagetypes() gives the image format and types supported by the current version of GD-PHP.
PHP parser parses the PHP developed website from the opening to the closing tag. Tags indicate that from where PHP code is being started and ended. In other words, opening and closing tags decide the scope of PHP scripting syntax of closing tag in PHP
syntax of closing tag in PHP
The necessary steps to create a MySQL database using PHP are:
Establish a connection to MySQL server from your PHP script. If the connection is successful, write a SQL query to create a database and store it in a string variable. Execute the query.
No, a parent constructor have to be called explicitly as follows:
__sleep returns the array of all the variables that need to be saved, while __wakeup retrieves them.
1- Combining two variables as follows:
$variable1 = 'Hello '; $variable2 = 'World'; $variable3 = $variable1.$variable2;
You can propagate a session id via cookies or URL parameters.
A persistent cookie is permanently stored in a cookie file on the browser's computer. By default, cookies are temporary and are erased if we close the browser.
Sessions automatically end when the PHP script finishes executing but can be manually ended using the session_write_close().
The session_unregister() function unregister a global variable from the current session and the session_unset() function frees all session variables.
$GLOBALS is associative array including references to all variables which are currently defined in the global scope of the script.
$_SERVER is an array including information created by the web server such as paths, headers, and script locations.
$_FILES is an associative array composed of items sent to the current script via the HTTP POST method.
$_FILES['userfile']['name'] represents the original name of the file on the client machine, $_FILES['userfile']['tmp_name'] represents the temporary filename of the file stored on the server.
$_ENV is an associative array of variables sent to the current PHP script via the environment method.
$a and $b: TRUE if both $a and $b are TRUE. $a & $b: Bits that are set in both $a and $b are set.
The first is the concatenation operator ('.'), which returns the concatenation of its right and left arguments. The second is ('.='), which appends the argument on the right to the argument on the left.
$a === $b TRUE if $a and $b have the same key/value pairs in the same order and of the same types.
!= means inequality (TRUE if $a is not equal to $b) and !== means non-identity (TRUE if $a is not identical to $b).
To be able to verify whether a PHP variable is an instantiated object of a certain class we use instanceof.
The goto statement can be placed to enable jumping inside the PHP program. The target is pointed by a label followed by a colon, and the instruction is specified as a goto statement followed by the desired target label.
Exception::getMessage lets us getting the Exception message and Exception::getLine lets us getting the line in which the exception occurred.
Exception::__toString gives the String representation of the exception.

Download Project

We are here for you !! ask your project to Implement - Contact US : 96 77 44 38 55

Have a question or need a custom quote?

Contact Us 9677443855