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
PHP Articles
Page 34 of 81
PHP Error Control Operator
In PHP, the @ symbol is defined as the Error Control Operator. When it is prefixed to any expression, any error encountered by PHP parser while executing it will be suppressed and the expression will be ignored. Example Without Error Control Operator Following code tries to open a non-existing file for read operation, but PHP parser reports warning − The output of the above code is − Hello World PHP Warning: fopen(nosuchfile.txt): failed to open stream: No such file or directory in /home/cg/root/1569997/main.php on line 2 Example With Error ...
Read MorePHP Expressions
Almost everything in a PHP script is an expression. Anything that has a value is an expression. In a typical assignment statement ($x=100), a literal value, a function or operands processed by operators is an expression − anything that appears to the right of assignment operator (=). Syntax $x = 100; // 100 is an expression $a = $b + $c; // $b + $c is an expression $c = add($a, $b); // add($a, $b) is an expression $val ...
Read MorePHP goto Statement
The goto statement in PHP allows you to transfer program control to a specific labeled location within your code. It's typically used within conditional statements like if, else, or switch constructs to control program flow. Syntax statement1; statement2; if (expression) goto label1; statement3; label1: statement4; After statement2, if the expression evaluates to true, program flow jumps to label1. If false, statement3 executes normally. The program then continues with normal execution flow. Example 1: Conditional Jump This example demonstrates jumping to a label based on whether a number is even ...
Read MorePHP Variable functions
Variable functions in PHP allow you to call a function dynamically by storing its name in a variable. When you append parentheses to a variable containing a function name, PHP executes that function. This feature is particularly useful for implementing callbacks, function tables, and dynamic function calls. Note: Variable functions cannot be used with language constructs like echo, include, require, etc. Use function wrappers as a workaround when needed. Basic Variable Function Here's a simple example where a variable stores a function name and calls it ? Hello World ...
Read MorePHP return Statement
The return statement in PHP is used to return control of program execution back to the environment from which it was called. Upon returning, execution continues with the statement following the function call. When a return statement occurs inside a function, execution of that function terminates immediately, passing control back to the calling code. The return statement can optionally include an expression, in which case the value of that expression is returned along with control. If a return statement is encountered in an included script, execution of that script ends immediately and control returns to the script that ...
Read MorePHP 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 More