Found 1060 Articles for PHP

What is the use of ini_set() in PHP?

Alok Prasad
Updated on 29-Jun-2020 12:50:11

6K+ Views

PHP allows the user to modify some of its settings mentioned in php.ini using ini_set(). This function requires two string arguments. The first one is the name of the setting to be modified and the second one is the new value to be assigned to it.Parametersvar nameNot all the available options can be changed using ini_set(). There is a list of all available options in the appendix.new valueThe new value for the option.ExampleGiven the line of code will enable the display_error setting for the script if it’s disabled. We need to put the above statement, at the top of the ... Read More

What is the meaning of a Persistent Cookie in PHP?

Alok Prasad
Updated on 29-Jun-2020 12:04:35

1K+ Views

A persistent cookie is a cookie that is stored in a cookie file permanently on the browser's computer. As we know cookies are small text files which are as a matter, of course, temporary cookies which stored only in the browser's memory. When the browser is closed, temporary cookies will be erased from the memory.When to use persistent cookies −Temporary cookies can not be used for tracking long-term information.Persistent cookies can be used for tracking long-term information.Temporary cookies are safer because no programs other than the browser can access them.Persistent cookies are less secure because users can open cookie files ... Read More

What is Exception Handling in PHP ?

Alok Prasad
Updated on 29-Jun-2020 12:05:32

323 Views

An exception is a problem that arised during the execution of a program. During the execution of a program when an exception occurs, code following the statement will not be executed, and PHP will attempt to find the first matching catch block. If an exception is not caught, a PHP Fatal Error will be issued with an “Uncaught Exception”.Syntax   try {       print "this is our try block";       throw new Exception();       }catch (Exception $e) {          print "something went wrong, caught yah! n";       }finally {   ... Read More

What is .htaccess in PHP?

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

8K+ Views

.htaccess is a configuration file for use on web servers running on the web apache server software. when a .htaccess file is placed in a directory which in turn loaded via the Apache web server, then the .htaccess file detected and executed by the Apache server software. .htaccess files can be utilized to modify the setup of the Apache server software to empower additional functionality and fetures that the apache web server softwatre brings to the table. We can use the .htaccess file for alteration various configuration in apache web server software. Some of them are listed below:ErrorDocumentsCreating custom error pages ... Read More

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

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

2K+ 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

3K+ 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

449 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

12K+ 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

4K+ 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.

Advertisements