Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Malhar Lathkar
Page 6 of 11
PHP require Statement
The require statement in PHP includes and executes a specified file. Unlike the include statement, if the required file is not found, PHP produces a fatal error and terminates the script execution. Syntax require "filename.php"; // or require("filename.php"); How require Works PHP searches for the file in the current directory first, then in directories specified in the include_path setting of php.ini. If the file is not found, PHP emits an E_COMPILE_ERROR and halts execution. Example Here's a simple example showing how require includes a file ?
Read MorePHP include_once Statement
The include_once statement in PHP includes and evaluates a specified file only once during script execution. Unlike the regular include statement, include_once prevents the same file from being loaded multiple times, which helps avoid redefinition errors and improves performance. The include_once statement is commonly used for loading configuration files, libraries, or setting up global variables that should only be initialized once during application execution. Syntax include_once 'filename.php'; // or include_once('filename.php'); Basic Example Here's a simple demonstration of how include_once works ? Main script started This content is ...
Read MorePHP Unsetting References
In PHP, you can break the binding between a variable and its reference using the unset() function. The unset() function doesn't destroy the content but only decouples the variable from its reference, allowing it to be used independently. Using unset() Function The most common way to unset a reference is using the unset() function ? before unsetting: 10 10 after unsetting: 10 20 After unsetting, $b can be used as a normal variable without affecting $a. Using NULL Assignment References can also be removed by assigning the variable ...
Read MorePHP Spotting References
In PHP, references create aliases between variables, allowing multiple variables to point to the same memory location. Understanding how references work is crucial for spotting and managing them effectively in your code. Understanding Reference Behavior When you create a reference to a global variable inside a function and then unset it, the original global variable remains intact. This demonstrates that unset() only removes the reference, not the actual variable ? Hello World, Hello World Hello PHP, Hello PHP Hello PHP Spotting References with debug_zval_dump() The debug_zval_dump() function helps you ...
Read MorePHP Return by Reference
In PHP, a function can return a reference instead of a value copy. This is useful when you want to allow external code to modify the original variable inside the function. To define a function that returns by reference, prefix its name with the & symbol. Syntax function &functionName() { // function body return $variable; // returns reference to $variable } $ref = &functionName(); // assign by reference Function Returning Reference The following example demonstrates a function that returns a reference to a static variable ...
Read MorePHP References
In PHP, references enable accessing the same variable content by different names. They are not like pointers in C/C++ as it is not possible to perform arithmetic operations using them. In C/C++, they are actual memory addresses. In PHP in contrast, they are symbol table aliases. In PHP, variable name and variable content are different, so the same content can have different names. A reference variable is created by prefixing & sign to original variable. Assign By Reference You can create a reference by using the & operator. When you assign $b = &$a, both variables point to ...
Read MorePHP Visibility modes
In PHP, visibility modes control the accessibility of class members (properties, methods, and constants). PHP provides three visibility keywords: public, private, and protected. Public members are accessible from anywhere, protected members can be accessed within the class and its subclasses, while private members are only accessible within the same class. Class public protected private ✓ Accessible everywhere ✓ Class + Subclasses only ...
Read MorePHP Late Static Bindings
Late static binding in PHP allows you to reference the called class in a static inheritance context rather than the class where the method is defined. When using static:: instead of self::, PHP resolves the class name at runtime based on the calling context. The Problem with self:: When static methods use self::, the class reference is resolved at compile time to the class where the method is defined ? name of class: test1 The output shows the parent class name because self:: was resolved to test1 at compile time. ...
Read MorePHP Object Serialization
Object serialization in PHP converts an object into a string representation (byte-stream) that can be stored or transmitted. The serialize() function creates this string containing all object properties, while unserialize() reconstructs the object from the string. Syntax // Serialize an object $serialized_string = serialize($object); // Unserialize back to object $object = unserialize($serialized_string); Basic Example Let's create a simple class and demonstrate serialization − Serialized: O:6:"Person":2:{s:11:"Personname";s:5:"Alice";s:10:"Personage";i:25;} Name: Alice Age: 25 Storing Serialized Objects in Files You can save serialized objects to files for persistent ...
Read MorePHP Scope Resolution Operator (::)
In PHP, the double colon :: is defined as Scope Resolution Operator. It is used when we want to access constants, properties and methods defined at class level. When referring to these items outside class definition, name of class is used along with scope resolution operator. This operator is also called Paamayim Nekudotayim, which in Hebrew means double colon. Syntax 3.142103.14210 Using self Keyword Inside Class To access class level items inside any method, keyword self is used − 3.142 10 Accessing ...
Read More