Found 1046 Articles for PHP

Program to find how many times a character appears in a string in PHP

Alok Prasad
Updated on 29-Jun-2020 11:59:22

1K+ Views

Example Live DemoOutputw appears 1 times e appears 2 times l appears 2 times c appears 1 times o appears 4 times m appears 1 times t appears 4 times u appears 1 times r appears 1 times i appears 2 times a appears 1 times s appears 1 times p appears 1 times n appears 1 times

What is traits in PHP?

Alok Prasad
Updated on 08-Nov-2023 10:43:18

2K+ Views

In 5.4 PHP version trait is introduced to PHP object-oriented programming. A trait is like class however it is only for grouping methods in a fine-grained and reliable way. It isn't permitted to instantiate a trait on its own. Traits are introduced to PHP 5.4 to overcome the problems of single inheritance. As we know in single inheritance class can only inherit from one other single class. In the case of trait, it enables a developer to reuse sets of methods freely in several independent classes living in different class hierarchies. Example Output Result of addition two numbers:8 ... Read More

Is there any advantage to using __construct() instead of the class's name for a constructor in PHP?

Alok Prasad
Updated on 29-Jun-2020 11:51:56

347 Views

Yes, there are several advantages to using the magic function __construct() instead of the class's name. Those are listed below −The magic function __construct is introduced in PHP 5.4. One advantage of using __construct() over ClassName() as a constructor is if you change the name of the class, you don't need to update the constructor which supports DRY(don't repeat yourself) concept.If you have a child class you can call parent::__construct()to call the parent constructor in an easy way.Example Live DemoOutputThe class "myclass" was initiated! In SubClass constructorNote"__CLASS__" is what’s called a magic constant, which, in this case, returns the name of ... Read More

What is dependency injection in PHP?

Alok Prasad
Updated on 29-Jun-2020 11:54:40

10K+ Views

Dependency injection is a procedure where one object supplies the dependencies of another object. Dependency Injection is a software design approach that allows avoiding hard-coding dependencies and makes it possible to change the dependencies both at runtime and compile time.There are numerous approaches to inject objects, here are couple generally known −Constructor InjectionIn this approach, we can inject an object through the class constructor.Example Live DemoOutput3Setter Injectionwhere you inject the object to your class through a setter function.ExampleBenefits of Dependency InjectionAdding a new dependency is as easy as adding a new setter method, which does not interfere with the existing code.Read More

How to Zip a directory in PHP?

Alok Prasad
Updated on 29-Jun-2020 11:47:35

2K+ Views

We can use PHP ZipArchive class in order to zipping and unzipping the folder in PHP. As of PHP 5.3, this class is inbuilt. For using in windows users need to enable php_zip.dll inside of php.ini.Example

What is singleton design concept in PHP?

Alok Prasad
Updated on 29-Jun-2020 11:38:58

3K+ Views

Singleton Pattern ensures that a class has only one instance and provides a global point to access it. It ensures that only one object is available all across the application in a controlled state. Singleton pattern provides a way to access its only object which can be accessed directly without the need to instantiate the object of the class.ExampleOutputconnection createdExplanationIn the above example as we are following a singleton pattern so the object $db2 can't be created. Only a single object will be created and i.e available all across the application.

How To enable GZIP Compression in PHP?

Alok Prasad
Updated on 29-Jun-2020 11:41:43

5K+ Views

GZIP Compression is a simple, effective way to save bandwidth and speed up PHP application. The mechanism runs behind the GZIP compression is described below −Step1The browser/client request for a file to the server.Step2The server sends a .zip file to the browser (index.html.zip) rather than plain old index.html in response, due to which the download time and bandwidth decreases.Step3After the execution of the above step, the browser downloads the zipped file, extracts it, and then shows it to the user. This loads the webpage very quickly.In the Apache server, we have to Add the following to .htaccess file to enable ... Read More

What does double question mark (??) operator mean in PHP ?

Alok Prasad
Updated on 29-Jun-2020 11:37:56

5K+ Views

PHP 7 has added a new operator double question mark (??) operator. In PHP 7, the double question mark(??) operator known as Null Coalescing Operator.It returns its first operand if it exists and is not NULL; otherwise, it returns its second operand. It evaluates from left to right. Null Coalescing operator also can be used in a chain format.Let's take the below example to demonstrate the double question mark (??) operator.ExampleOutput9ExampleOutput34

How to convert an array to SimpleXML in PHP?

Alok Prasad
Updated on 29-Jun-2020 11:20:10

3K+ Views

We can solved the above problem using array_walk_recursive() function.array_walk_recursive()   is an inbuilt PHP function. This function converts array to XML document where keys of the array are converted into values and values of the array are converted into the element of XML.Let' demonstrate with a simple example.ExampleOutput alex account michigan NoteIf errors message display like PHP Fatal error: Uncaught Error: Class 'SimpleXMLElement' not found in then simply install php-xml, php-simplexml packages.

How to validate an email address in PHP ?

Alok Prasad
Updated on 29-Jun-2020 11:21:32

8K+ Views

In this article, we will learn to validate an email with PHP regular expression. We will learn different methods to validate email address in PHP.Method1The function preg_match() checks the input matching with patterns using regular expressions.Example Live DemoOutputValid email address.In the above example PHP preg_match() function has been used to search string for a pattern and PHP ternary operator has been used to return the true or false value based on the preg_match return.Method 2We will discuss email validation using filter_var() method.Example Live DemoOutputpattrick@tutorialspoint.com is a valid email addressRead More

Advertisements