strip_tags() function is used to retrieve the string from a text by omitting HTML, XML and PHP tags. This function has one mandatory parameter and one optional parameter. The optional parameter is used to accept particular tags.
//Remove all tags from the text
echo strip_tags("<b>PHP</b> is a popular <em>scripting</em> language");
//Remove all tags excluding <b> tag
echo strip_tags("<b>PHP</b> is a popular <em>scripting</em> language","<b>");
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
In PHP, type hinting is used to specify the expected data type (arrays, objects, interface, etc.) for an argument in a function declaration. It was introduced in PHP 5.
Whenever the function is called, PHP checks if the arguments are of a user-preferred type or not. If the argument is not of the specified type, the run time will display an error and the program will not execute.
It is helpful in better code organization and improved error messages.
There is not a way to directly delete a cookie. Just use the setcookie function with the expiration date in the past, to trigger the removal mechanism in your web browser.
In PHP, echo is used to print data on the webpage.
Example: <?php echo ‘Car insurance’; ?>
unlink() function is used in PHP to delete any file.
The final keyword in a declaration of the method indicates that the method cannot be overridden by subclasses. A class that is declared as final cannot be subclassed.
This is especially useful when we are creating an immutable class like the String class. Only classes and methods may be declared final, properties cannot be declared as final.
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.
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.
imagetypes() gives the image format and types supported by the current version of GD-PHP.
The basic steps to create MySQL database using PHP are:
1. Establish a connection to MySQL server from your PHP script.
2. If the connection is successful, write a SQL query to create a database and store it in a string variable.
3. Execute the query.
There are three types of array in PHP:
1. Indexed array: an array with a numeric key.
2. Associative array: an array where each key has its specific value.
3. Multidimensional array: an array containing one or more arrays within itself.
The GD library is required to be able to do image functions. It also helps to execute more image functions.
A cookie is a small piece of information stored in a client browser. It is a technique utilized to identify a user using the information stored in their browser. Utilizing PHP, we can both set and get COOKIE.
A session is a super global variable that preserves data across subsequent pages. Session uniquely defines all users with a session ID. So it supports building a customized web application where user tracking is required.
PDO stands for PHP Data Object. PDO is a set of PHP extensions that provide a core PDO class and database, specific drivers. The PDO extension can access any database which is written for the PDO driver. There are several PDO drivers available which are used for FreeTDS, Microsoft SQL Server, IBM DB2, Sybase, Oracle Call Interface, Firebird/Interbase 6 and PostgreSQL databases, etc.
It gives a lightweight, vendor-neutral, data-access abstraction layer. Hence, no matter what database we use, the function to issue queries and fetch data will be the same. And, it focuses on data access abstraction instead of database abstraction.
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.
You need to change the value of the max_execution_time directive in the php.ini file for increasing the maximum execution time.
if you want to set the max execution time for 130 seconds, then set the value as follows,
max_execution_time = 130
gettype() function is used to check the data type of any variable.
Sample cecho gettype(true).''; //boolean
echo gettype(10).''; //integer
echo gettype('Web Programming').''; //string
echo gettype(null).''; //NULLode:
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 '==='.
PHP supports only single inheritance; it means that a class can be extended from only one single class using the keyword 'extended'.
Answer: You have to use PHP command in the command line to execute a PHP script. If the PHP file name is test.php then the following command is used to run the script from the command line.
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.
$var = "Hello"; //String
$var = 10; //Integer
There are several benefits of using PHP. First of all, it is totally free to use. So anyone can use PHP without any cost and host the site at a minimal cost.
It supports multiple databases. The most commonly used database is MySQL which is also free to use. Many PHP frameworks are used now for web development, such as CodeIgniter, CakePHP, Laravel, etc.
You can send an e-mail in PHP with mail() function or SMTP details.
count() function is used for fetching the total number records in a table.
They allow making the result of the expression between the tags directly to the browser response.
The 3 main types of errors in PHP are:
1. Notices: Notices are non-critical errors that can occur during the execution of the script. These are not visible to users. Example: Accessing an undefined variable.
2. Warnings: These are more critical than notices. Warnings don’t interrupt the script execution. By default, these are visible to the user. Example: include() a file that doesn’t exist.
3. Fatal: This is the most critical error type which, when occurs, immediately terminates the execution of the script. Example: Accessing a property of a non-existent object or require() a non-existent file.
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 session_start() function is used to start a new session. Also, it can resume an existing session if it is stopped. In this particular case, the return will be the current session if resumed.
The session_destroy() function is used to destroy all of the session variables as given below:
There are 3 types of Arrays in PHP:
1. Indexed Array – An array with a numeric index is known as the indexed array. Values are stored and accessed in linear fashion.
2, Associative Array – An array with strings as index is known as the associative array. This stores element values in association with key values rather than in a strict linear index order.
3. Multidimensional Array – An array containing one or more arrays is known as multidimensional array. The values are accessed using multiple indices.
The crypt() function is used for this functionality as it provides a large number of hashing algorithms that can be used. These algorithms include sha1, sha256, or md5 which are designed to be very fast and efficient.
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.
NULL is a particular type which contains only one value: NULL. If you need any variable to set NULL, just assign it.
Traits are a mechanism that lets you create reusable code in PHP and similar languages where multiple inheritances are not supported. It’s not possible to instantiate it on its own.
A trait is intended to reduce the limitations of single inheritance by enabling a developer to reuse sets of methods freely in many independent classes living in different hierarchies of class.
The cons of PHP are:
1. PHP is not suitable for giant content-based web applications.
2. Since it is open-source, it is not secure. Because ASCII text files are easily available.
3. Change or modification in the core behavior of online applications is not allowed by PHP.
4, If we use more features of the PHP framework and tools, it will cause poor performance of online applications.
5. PHP features a poor quality of handling errors. PHP lacks debugging tools, which are needed to look for warnings and errors. It has only a few debugging tools in comparison to other programming languages.
The final keyword in a method declaration indicates that the method cannot be overridden by subclasses. A class that is declared final cannot be subclassed. This is particularly useful when we are creating an immutable class like the String class.Properties cannot be declared final, only classes and methods may be declared as final.
The PHP syntax relates Perl and C.
A PHP parser is software that converts source code into the code that computer can understand. This means whatever set of instructions we give in the form of PHP code is converted into a machine-readable format by the parser.
PHP scripts have the ability to generate HTML, and it is possible to pass information from HTML to PHP.
PHP is a server-side language whereas HTML is a client-side language. So PHP executes on the server-side and gets its results as strings, objects, arrays, and then we use them to display its values in HTML.
This interaction helps bridge the gaps and use the best of both languages.
The following two rules are needed to be followed while naming a PHP variable:
1. It evaluates to FALSE in a Boolean context.
2. It returns FALSE when tested with IsSet() function.
To display the output directly to the browser, I will use the special tags <?= and ?>.
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.
PEAR stands for “PHP Extension and Application Repository”. PEAR is a framework and repository for all of the reusable PHP components.
PEAR provides a higher level of programming for web developers. It contains all kinds of PHP code snippets and libraries. It also provides you with a command-line interface to automatically install packages.
PHP is partially case sensitive. The variable names are case-sensitive but function names are not. If you define the function name in lowercase and call them in uppercase, it will still work. User-defined functions are not case sensitive but the rest of the language is case-sensitive.
To execute a PHP script, use the PHP Command Line Interface (CLI) and specify the file name of the script in the following way:
The PHP Data Objects (PDO) is a lightweight, and consistent interface used to access databases in PHP.
for, while, do while and foreach.
PEAR is a framework and repository for reusable PHP components. PEAR stands for PHP Extension and Application Repository. It contains all types of PHP code snippets and libraries. It also provides a command line interface to install “packages” automatically.
PHP is a widely-used, open-source server-side scripting language. PHP is an acronym for “PHP: Hypertext Preprocessor.” It allows the developers to develop dynamic web applications. PHP has various frameworks and CMS for developing dynamic and interactive websites.