Programming Articles

Page 1063 of 2547

How to validate an email address in PHP ?

Alok Prasad
Alok Prasad
Updated on 15-Mar-2026 9K+ Views

In PHP, validating email addresses is essential for ensuring data integrity and preventing invalid inputs. PHP provides several methods to validate email addresses, from built-in functions to custom regular expressions. Using filter_var() Function The most recommended approach is using PHP's built-in filter_var() function with the FILTER_VALIDATE_EMAIL filter − pattrick@tutorialspoint.com is a valid email address Using Regular Expression with preg_match() You can also create a custom validation function using regular expressions with preg_match() − Valid email address. Testing Invalid Email Examples ...

Read More

What is PHP Output Buffering?

Alok Prasad
Alok Prasad
Updated on 15-Mar-2026 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 sends 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. Basic Example Let's demonstrate with a simple example ? string(5) "Hello" string(20) "HelloTutorials Point" How It Works In the above example, ob_get_contents() grabs all of ...

Read More

How to remove non-alphanumeric characters in PHP stirng?

Alok Prasad
Alok Prasad
Updated on 15-Mar-2026 3K+ Views

We can remove non-alphanumeric characters from a string using the preg_replace() function in PHP. The preg_replace() function is an inbuilt function that performs regular expression search and replace operations. Syntax preg_replace(pattern, replacement, subject, limit, count) Parameters pattern: The regular expression pattern to search for. replacement: The string or array to replace matched patterns with. subject: The string or array to search and replace in. limit: Optional. Maximum possible replacements for each pattern. count: Optional. Variable that will contain the number of replacements performed. Example Let's demonstrate how to ...

Read More

Compare define() vs const in PHP

Alok Prasad
Alok Prasad
Updated on 15-Mar-2026 5K+ Views

In PHP, both define() and const are used to declare constants, but they have important differences in when and how they can be used. Syntax FOO BAR Key Differences Compile-time vs Runtime Definition The basic difference is that const defines constants at compile time, whereas define() defines them at run time ? Available immediately Available after execution Conditional Definition We can't use const in conditional blocks, while define() allows conditional constant declaration ? This ...

Read More

How to call parent constructor in child class in PHP?

Alok Prasad
Alok Prasad
Updated on 15-Mar-2026 4K+ Views

In PHP object-oriented programming, there are two scenarios when calling parent constructors in child classes, depending on whether the child class defines its own constructor. Case 1: Child Class Has Its Own Constructor When a child class defines its own constructor, the parent constructor is not automatically called. You must explicitly call parent::__construct() within the child constructor to execute the parent's initialization code ? I am in Tutorials Point I am not in Tutorials Point Explanation In the above example, parent::__construct() explicitly calls the parent class constructor before executing ...

Read More

How to Check if PHP session has already started?

Alok Prasad
Alok Prasad
Updated on 15-Mar-2026 4K+ Views

In PHP, we utilize session_start() to start a session. However, calling this function multiple times in the same script throws an error. Here we will learn how to check if a session has already started without calling session_start() twice. There are two methods to resolve this problem, depending on your PHP version. Method 1: Using session_id() (PHP < 5.4.0) For PHP versions below 5.4.0, use the session_id() function to check if a session exists − The session_id() function returns an empty string if no session has been started, making it perfect for ...

Read More

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

Alok Prasad
Alok Prasad
Updated on 15-Mar-2026 7K+ Views

In PHP, you can pass arguments to functions using two methods: pass by value (default) and pass by reference. Understanding the difference is crucial for effective programming. By default, PHP uses pass by value, meaning the function receives a copy of the variable. Changes inside the function don't affect the original variable. However, pass by reference allows the function to modify the original variable by using the ampersand (&) symbol. Pass By Reference To pass a variable by reference, prepend an ampersand (&) to the parameter name in the function definition. This allows the function to modify ...

Read More

What are getters and setters methods in PHP?

Alok Prasad
Alok Prasad
Updated on 15-Mar-2026 15K+ Views

In this article, we learn the best way to create getter and setter methods in PHP. Getter and setter methods are utilized when we need to restrict direct access to 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. Why Use Private Properties? Let's first understand why direct access to private properties ...

Read More

Explain Polymorphism in PHP.

Alok Prasad
Alok Prasad
Updated on 15-Mar-2026 13K+ Views

Polymorphism is derived from the Greek words "Poly" (meaning many) and "morphism" (meaning forms). In object-oriented programming, polymorphism allows methods in different classes that perform similar functions to share the same interface, enabling code to work with objects of different classes through a common interface. In PHP, polymorphism is implemented using interfaces or abstract classes. This ensures that different classes can be used interchangeably as long as they implement the same contract. Using Interfaces for Polymorphism An interface defines method signatures without implementation. Classes that implement an interface must provide implementations for all methods declared in the ...

Read More

What is method overloading in PHP?

Alok Prasad
Alok Prasad
Updated on 15-Mar-2026 11K+ Views

Method overloading is a concept in Object Oriented Programming that allows creating multiple methods with the same name that behave differently based on the number or type of parameters they accept. While traditional method overloading (static polymorphism) isn't directly supported in PHP, we can achieve similar functionality using PHP's magic methods. Traditional Method Overloading Issue Unlike other programming languages, PHP doesn't support traditional method overloading. The following example demonstrates this limitation ? Fatal error: Cannot redeclare machine::doTask() This error occurs because PHP treats both methods as duplicate declarations, regardless of ...

Read More
Showing 10621–10630 of 25,466 articles
Advertisements