It is an open source server-side scripting language which is widely used for web development. It supports many databases like MySQL, Oracle, Sybase, Solid, PostgreSQL, generic ODBC etc. PHP stands for PHP: Hypertext Pre-processor, that earlier stood for Personal Home Pages. Rasmus Lerdorf known as the father of PHP.
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.
'final' is introduced in PHP5. Final class means that this class cannot be extended and a final method cannot be overridden.
PHP syntax resembles Perl and C.
PEAR means "PHP Extension and Application Repository". It extends PHP and provides a higher level of programming for web developers.
PHP means-> PHP: Hypertext Preprocessor.
At begining PHP known as "Personal Home Page"
Current Version 7.4 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
The two most common ways to start and finish a PHP script are:
<?php [ --- PHP code---- ] ?> and <? [--- PHP code ---] ?>
To be able to display the output directly to the browser, we have to use the special tags <?= and ?>.
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 'extends'.
'final' is introduced in PHP5. Final class means that this class cannot be extended and a final method cannot be overridden.
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().
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.
To be able to display a human-readable result we use print_r().
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.
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 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.
A session is a logical object enabling us to preserve temporary data across multiple PHP pages.
The use of the function session_start() lets us activating a session.
You can propagate a session id via cookies or URL parameters.
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.
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.
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:
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:
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.
Syntax:
The $var (single dollar) is a normal variable with the name var that stores any value like string, integer, float, etc.
The $$var (double dollar) is a reference variable that stores the value of the $variable inside it.
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 '==='.
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().
The result set can be handled using mysqli_fetch_array, mysqli_fetch_assoc, mysqli_fetch_object or mysqli_fetch_row.
The unset() function is dedicated for variable management. It will make a variable undefined.
In PHP, objects passed by value.
No, a parent constructor have to be called explicitly as follows:
parent::constructor($value)
A session is a logical object enabling us to preserve temporary data across multiple PHP pages.
The use of the function session_start() lets us activating a session.
You can propagate a session id via cookies or URL parameters.
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.
$_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.
$_ENV is an associative array of variables sent to the current PHP script via the environment method.
$_COOKIE is an associative array of variables sent to the current PHP script using the HTTP Cookies
The scope of a variable is the context within which it is defined. For the most part, all PHP variables only have a single scope. This single scope spans included and required files as well.
PHP has borrowed its syntax from Perl and C.
A PHP variable is the name of the memory location that holds data. It is temporary storage.
Syntax:
PHP magic constants are predefined constants, which change based on their use. They start with a double underscore (__) and end with a double underscore (__).
The PHP count() function is used to count total elements in the array, or something an object.
The header() function is used to send a raw HTTP header to a client. It must be called before sending the actual output. For example, you can't print any HTML element before using this function.
The isset() function checks if the variable is defined and not null.
An array is used to store multiple values in a single value. In PHP, it orders maps of pairs of keys and values. It saves the collection of the data type.
There are three types of array in PHP:
There are many array functions in PHP:
The indexed array holds elements in an indexed form which is represented by number starting from 0 and incremented by 1. For example:
The associative array holds elements with name. For example:
The strlen() function is used to get the length of the string.
There are many array functions in PHP:
There are two methods GET and POST.
You can use JavaScript submit() function to submit the form without explicitly clicking any submit button.
PHP allows you to include file so that page content can be reused again. There are two ways to add the file in PHP.
Require and include both are used to include a file, but if data is not found include sends warning whereas require sends Fatal error.
PHP setcookie() function is used to set cookie with HTTP response. Once the cookie is set, you can access it by $_COOKIE superglobal variable.
Syntax:
PHP Engine creates a logical object to preserve data across subsequent HTTP requests, which is known as session.
Sessions generally store temporary data to allow multiple PHP pages to offer a complete functional transaction for the same user.
Simply, it maintains data of an user (browser).
PHP session_start() function is used to start the session. It starts new or resumes the current session. It returns the current session if the session is created already. If the session is not available, it creates and returns new sessions.
The main difference between session and cookie is that cookies are stored on user's computer in the text file format while sessions are stored on the server side.
Cookies can't hold multiple variables, on the other hand, Session can hold multiple variables.
You can manually set an expiry for a cookie, while session only remains active as long as browser is open.
PHP fopen() function is used to open file or URL and returns resource. It accepts two arguments: $filename and $mode.
Syntax:
PHP provides various functions to read data from the file. Different functions allow you to read all file data, read data line by line, and read data character by character.
PHP file read functions are given below:
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.
The readfile() function is used to download the file in PHP.
The mail() function is used to send email in PHP.
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.
Since PHP 4.3, mysql_reate_db() is deprecated. Now you can use the following 2 alternatives.
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.
The exit() function is used to stop the execution of PHP script.
CRYPT() and MD5()
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.
The Include() function is used to put data of one PHP file into another PHP file. If errors occur, then the include() function produces a warning but does not stop the execution of the script, and it will continue to execute.
The Require() function is also used to put data of one PHP file to another PHP file. If there are any errors, then the require() function produces a warning and a fatal error and stops the script.
A cookie is used to identify a user. A cookie is a little record that the server installs on the client's Computer. Each time a similar PC asks for a page with a program, it will send the cookie as well. With PHP, you can both make and recover cookie value.
Some important points regarding Cookies:
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
<?php syntax of opening tag in PHP
?> syntax of closing tag in PHP
The necessary steps to create a MySQL database using PHP are:
Join online php and python class at sstech lab noida.
Answer: No. PHP is a weakly typed or loosely typed language.
This means PHP does not require to declare data types of the variable when you declare any variable like the other standard programming languages C# or Java. When you store any string value in a variable, then the data type is the string and if you store a numeric value in that same variable then the data type is an Integer.
Sample code:
$var = "Hello" ; //String $var = 10; //Integer |
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.
PHP has a total of eight data types which we use to construct our variables −
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).
NULL is a special type that only has one value: NULL. To give a variable the NULL value, simply assign it like this −
$my_var = NULL;
The special constant NULL is capitalized by convention, but actually it is case insensitive; you could just as well have typed −
$my_var = null;
A variable that has been assigned NULL has the following properties:
It evaluates to FALSE in a Boolean context.
It returns FALSE when tested with IsSet() function.
To define a constant you have to use define() function and to retrieve the value of a constant, you have to simply specifying its name. Unlike with variables, you do not need to have a constant with a $.
As indicated by the name, this function will return the value of the constant. This is useful when you want to retrieve value of a constant, but you do not know its name, i.e. It is stored in a variable or returned by a function.
<?php
define("MINSIZE", 50);
echo MINSIZE;
echo constant("MINSIZE"); // same thing as the previous line
?>
Only scalar data (boolean, integer, float and string) can be contained in constants.
There is no need to write a dollar sign ($) before a constant, where as in Variable one has to write a dollar sign.
Constants cannot be defined by simple assignment, they may only be defined using the define() function.
Constants may be defined and accessed anywhere without regard to variable scoping rules.
Once the Constants have been set, may not be redefined or undefined.
The PHP $_REQUEST variable contains the contents of both $_GET, $_POST, and $_COOKIE. We will discuss $_COOKIE variable when we will explain about cookies. The PHP $_REQUEST variable can be used to get the result from form data sent with both the GET and POST methods.
Singly quoted strings are treated almost literally, whereas doubly quoted strings replace variables with their values as well as specially interpreting certain character sequences.
<?php $variable = "name"; $literally = 'My $variable will not print!\n'; print($literally); print "<br />"; $literally = "My $variable will print!\n"; print($literally); ?>
This will produce following result −
My $variable will not print! My name will print
Here is the example to unset a single variable −
<?php
unset($_SESSION['counter']);
?>
This is a global PHP variable. This variable is an associate double dimension array and keeps all the information related to uploaded file.
$_SERVER − This is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server. There is no guarantee that every web server will provide any of these.
The following table lists the most important elements that can go inside $_SERVER:
Element/Code | Description |
---|---|
$_SERVER['PHP_SELF'] | Returns the filename of the currently executing script |
$_SERVER['GATEWAY_INTERFACE'] | Returns the version of the Common Gateway Interface (CGI) the server is using |
$_SERVER['SERVER_ADDR'] | Returns the IP address of the host server |
$_SERVER['SERVER_NAME'] | Returns the name of the host server (such as www.sstechlab.com) |
$_SERVER['SERVER_SOFTWARE'] | Returns the server identification string (such as Apache/2.2.24) |
$_SERVER['SERVER_PROTOCOL'] | Returns the name and revision of the information protocol (such as HTTP/1.1) |
$_SERVER['REQUEST_METHOD'] | Returns the request method used to access the page (such as POST) |
$_SERVER['REQUEST_TIME'] | Returns the timestamp of the start of the request (such as 1377687496) |
$_SERVER['QUERY_STRING'] | Returns the query string if the page is accessed via a query string |
$_SERVER['HTTP_ACCEPT'] | Returns the Accept header from the current request |
$_SERVER['HTTP_ACCEPT_CHARSET'] | Returns the Accept_Charset header from the current request (such as utf-8,ISO-8859-1) |
$_SERVER['HTTP_HOST'] | Returns the Host header from the current request |
$_SERVER['HTTP_REFERER'] | Returns the complete URL of the current page (not reliable because not all user-agents support it) |
$_SERVER['HTTPS'] | Is the script queried through a secure HTTP protocol |
$_SERVER['REMOTE_ADDR'] | Returns the IP address from where the user is viewing the current page |
$_SERVER['REMOTE_HOST'] | Returns the Host name from where the user is viewing the current page |
$_SERVER['REMOTE_PORT'] | Returns the port being used on the user's machine to communicate with the web server |
$_SERVER['SCRIPT_FILENAME'] | Returns the absolute pathname of the currently executing script |
$_SERVER['SERVER_ADMIN'] | Returns the value given to the SERVER_ADMIN directive in the web server configuration file (if your script runs on a virtual host, it will be the value defined for that virtual host) (such as someone@sstechlab.com) |
$_SERVER['SERVER_PORT'] | Returns the port on the server machine being used by the web server for communication (such as 80) |
$_SERVER['SERVER_SIGNATURE'] | Returns the server version and virtual host name which are added to server-generated pages |
$_SERVER['PATH_TRANSLATED'] | Returns the file system based path to the current script |
$_SERVER['SCRIPT_NAME'] | Returns the path of the current script |
$_SERVER['SCRIPT_URI'] | Returns the URI of the current page |
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
<?php syntax of opening tag in PHP
?> syntax of closing tag in PHP