Found 1046 Articles for PHP

What is PHP Output Buffering?

Alok Prasad
Updated on 29-Jun-2020 11:23:08

3K+ Views

Output Buffering is a method to tell the PHP engine to hold the output data before sending it to the browser. As we know PHP sent the output data to the browser in pieces, but if we utilize the output buffering mechanism, the output data is stored in a variable and sent to the browser as one piece at the end of the script.ExampleLet's demonstrate with a simple example. Live DemoOutputstring(5) "Hello" string(20) "HelloTutorials Point"ExplanationIn the above example ob_get_contents() grabs all of the data gathered since we called ob_start, i.e. everything in the buffer. After that send the output data at ... Read More

How to remove non-alphanumeric characters in PHP stirng?

Alok Prasad
Updated on 29-Jun-2020 11:24:11

2K+ Views

We can remove non-alphanumeric characters from the string with preg_replace() function in PHP. The preg_replace() function is an inbuilt function in PHP which is used to perform a regular expression for search and replace the content.syntaxpreg_replace(pattern, replacement, subject, limit, count )Let's discuss the parameters of the function underneath.patternThis parameter contains the pattern to search for.replacementIt is a mandatory parameter. This parameter may contain a string or an array with strings to replace.subjectThe string or an array with strings to search and replace.limitThe maximum possible replacements for each pattern in each subject stringcountThis is an optional parameter, if specified then this ... Read More

Compare define() vs const in PHP

Alok Prasad
Updated on 31-Dec-2019 10:10:49

4K+ Views

As we know both define() and const are used to declare a constant in PHP script.SyntaxLet's discuss the difference between these two.The basic difference between these two is that const defines constants at compile time, whereas define() defines them at run time.We can't use the const keyword to declare constant in conditional blocks, while with define() we can achieve that.const accepts a static scalar(number, string or other constants like true, false, null, __FILE__), whereas define() takes any expression.consts are always case sensitive, whereas define() allows you to define case insensitive constants by passing true as the third argument.const can also ... Read More

How to convert XML file into array in PHP?

Alok Prasad
Updated on 29-Jun-2020 11:04:27

2K+ Views

To convert the XML document into PHP array, we have to utilize some PHP functions. The procedure is explained underneath with an example.Step 1We have to create an XML file that needs to convert into the array.abc.xml           AL       A                    SA          S     Step 2The above XML file will import into PHP using file_get_contents() function which read the entire file as a string and stores into a variable.Step 3After the above step, we ... Read More

How to call parent constructor in child class in PHP?

Alok Prasad
Updated on 29-Jun-2020 11:06:57

4K+ Views

We will face two cases while calling the parent constructor method in child class.Case1We can't run directly the parent class constructor in child class if the child class defines a constructor. In order to run a parent constructor, a call to parent::__construct() within the child constructor is required.Example Live DemoOutput: I am in Tutorials Point I am not in Tutorials PointExplanationIn the above example, we have used parent::__construct() to call the parent class constructor.Case2If the child does not define a constructor then it may be inherited from the parent class just like a normal class method(if it was not declared as ... Read More

How to Check if PHP session has already started?

Alok Prasad
Updated on 29-Jun-2020 09:48:01

3K+ Views

In PHP, we utilize session_start() an inbuilt function to start the session .But the problem we face in a PHP script is if we execute it more than once it throws an error. So here we will learn how to check the session started or not without calling session_start() function twice.There are two ways to follow to resolve this problem.For below PHP 5.4.0 version.ExampleExplanationIf the session not started this code above will always start the session in the PHP script.In the second method, we can utilize the function session_status(), which returns the status of the present session. This function can ... Read More

What is Pass By Reference and Pass By Value in PHP?

Alok Prasad
Updated on 29-Jun-2020 08:51:12

5K+ Views

In this article, we will learn about pass by value and pass by reference in PHP. Now, let’s understand these two concepts in detail.In PHP generally, we followed to pass the arguments to the function with passed by value approach. We are following this practice because if the value of the argument within the function is changed, it does not get changed outside of the function.In some cases we may need to modify function arguments, So to allow a function to modify its arguments, they must be passed by reference.Let's begin with passed by reference. As it is already mentioned we ... Read More

What are getters and setters methods in PHP?

Alok Prasad
Updated on 31-Dec-2019 08:57:06

14K+ Views

In this article, we learn the best way to create getter and setter strategies in PHP. Getter and setter strategies are utilized when we need to restrict the direct access to the variables by end-users. Getters and setters are methods used to define or retrieve the values of variables, normally private ones.Just as the name suggests, a getter method is a technique that gets or recovers the value of an object. Also, a setter method is a technique that sets the value of an object.ExampleLet's understand the use of getter and setter methods through an example.Output:PHP Error Cannot access private ... Read More

Explain Polymorphism in PHP.

Alok Prasad
Updated on 30-Jul-2019 22:30:26

10K+ Views

To begin with, Polymorphism is gotten from the Greek words Poly (which means many) and morphism (which meaning forms).Polymorphism portrays an example in object-oriented programming where methods in various classes that do similar things should have a similar name. Polymorphism is essentially an OOP pattern that enables numerous classes with different functionalities to execute or share a commonInterface. The usefulness of polymorphism is code written in different classes doesn't have any effect which class it belongs because they are used in the same way. In order to ensure that the classes do implement the polymorphism guideline, we can pick between ... Read More

How to display errors in PHP file?

Alok Prasad
Updated on 07-Oct-2023 02:49:53

36K+ Views

A PHP application produces many levels of errors during the runtime of the script . So in this article, we will learn how to display all the errors and warning messages. The quickest way to display all php errors and warnings is to add these lines to your PHP code file: ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); The ini_set function will try to override the configuration found in your php.ini file. If in the php.ini file display_error is turned off it will turn that on in the code. It also set display_startup_errors to true to show the error message. error_reporting() ... Read More

Advertisements