Found 1046 Articles for PHP

PHP return Statement

Malhar Lathkar
Updated on 18-Sep-2020 13:30:52

2K+ Views

IntroductionThe purpose of return statement in PHP is to return control of program execution back to the environment from which it was called. Upon returning, execution of expression following the one which invooked other function or module.If return statement occurs inside a function, execution of current function is terminated, handing over the control back to the environment from which it was called. The return statement may have an exprssion as optional clause in front of it. In that case, value of the expression is also returned in addition to the control.If encountered in an included script, execution of current scripts ... Read More

PHP require_once Statement

Malhar Lathkar
Updated on 18-Sep-2020 13:23:49

236 Views

IntroductionThe require_once statement in PHP is similar in functioning to that of require statement. Only difference is, if a file has been already included for processing, it will not be included again.Note that a file included with either include or require statement will also be not included again even if require_once statement is used.Other behaviour of require_once statement is similar to require statement .require_once ExampleIn following example main php script includes test.phpExample Live Demo //test.php OutputThis will produce following result when main script is run from command line −inside main script now including with require_once test.php script File included try to include ... Read More

PHP require Statement

Malhar Lathkar
Updated on 18-Sep-2020 13:22:25

210 Views

IntroductionEffect of require statement is similar to include statement in PHP. However, there is one main difference. If the parser fails to find required file, it produces a fatal error thereby termination of current script. The include statement on the other hand emits a warning in case of failure to find file and execution of current script continues.PHP parser tries to locate the file in current folder by default and further in directories mentioned in include_path setting of php.ini, as in case of include statement. If file asked for is not available in current folder as well as include_path folders, ... Read More

PHP include_once Statement

Malhar Lathkar
Updated on 18-Sep-2020 13:17:27

221 Views

IntroductionJust as the include statement, include_once also transfers and evaluates script written in one file in another, the difference in two lies in the fact that include_once prevents same file from reloading if already done. As the name indicates, a file will be included only once even if one tries to issue include instruction again.The include_once statement is typically used to set up global variables, enable libraries or any such activity that is expected to be done in the beginning of execution of application.Other than this, behaviour of include_once statement is similar to include statement.include_once ExampleIn following example testscript.php is ... Read More

PHP Unsetting References

Malhar Lathkar
Updated on 18-Sep-2020 12:18:59

215 Views

IntroductionIt is possible to break binding between the content and variable by using unset() function. The unset() function doesn't destroy the content but only decouples variable from it.Example Live DemoOutputAfter unsetting, $b can be used as normal vaiablebefore unsetting : 10 10 after unsetting : 10 20Reference can also be removed by assigning variable to NULLExample Live DemoOutputResult of above script is as followsx and y are references 100 x: 200 y:

PHP Spotting References

Malhar Lathkar
Updated on 18-Sep-2020 12:17:24

150 Views

IntroductionMany syntax constructs in PHP are implemented via referencing mechanisms. If reference to a global variable is unset in a function, the same variable in global namespace is not removed.Example Live DemoOutputGlobal $va1 is intact.Hello World, Hello World Hello PHP, Hello PHP Hello PHPdebug_zval_dump() function can be used if a variable has references to other variables

PHP Return by Reference

Malhar Lathkar
Updated on 18-Sep-2020 12:16:07

745 Views

IntroductionIn PHP,   a function can also be made to return a reference. This is useful  to find to which variable a reference should be bound. To define a function which returns reference, prefix its name by & sign.ExampleIn following example, myfunction() is defined to return by reference. It contains a static variable whose reference is returned and assigned to a global variable. Value of local static variable will also change its reference ouside is assigned with different value.Example Live DemoOutputThis example gives following outputx Inside function: 10 returned by reference: 10 x Inside function: 20method returning referenceA class can also ... Read More

PHP Passing by Reference

Malhar Lathkar
Updated on 18-Sep-2020 12:13:27

10K+ Views

IntroductionIn PHP,  arguments to a function can be passed by value or passed by reference. By default, values of actual arguments are passed by value to formal arguments which become local variables inside the function. Hence, modification to these variables doesn't change value of actual argument variable.When arguments are passed by reference, change in value of formal argument is reflected in actual argument variable because the former is a reference to latter. Thus pass by reference mechanism helps in indirectly manipulating data in global space. It also helps in overcoming the fact that a function can return only one variable.Pass ... Read More

PHP References

Malhar Lathkar
Updated on 18-Sep-2020 12:10:17

3K+ Views

IntroductionIn 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 oprations 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. Hence $b=&$a will mean that $b is a referewnce variable of $a.Assign By ReferenceIn following example, two variables refer to same valueExample Live DemoOutputChange in value ... Read More

PHP $_SERVER

Malhar Lathkar
Updated on 18-Sep-2020 12:05:38

4K+ Views

Introduction$_SERVER is a superglobal that holds information regarding HTTP headers, path and script location etc. All the server and execution environment related information is available in this associative array. Most of the entries in this array are populated by web server.PHP versions prior to 5.4.0 contained $HTTP_SERVER_VARS contained same information but has now been removed. Following are some prominent members of this arrayPHP_SELF − stores filename of currently executing script. For example, a script in test folder of document root of a local server returns its path as follows −ExampleThis results in following output in browser with http://localhost/test/testscript.php URL/test/testscript.phpSERVER_ADDR − This ... Read More

Advertisements