FAQs
| Browse in : |
All
> FAQs
> PHP |
- Ampersand and Passing by Reference
- Conversion Specification Type Codes
- ECHO, PRINT, PRINTF, and SPRINTF Differences
- ISSET() vs. EMPTY()
- PHP Image Functions
- Regular Expressions (REGEX)
- REQUIRE() vs INCLUDE()
- User Specified Array Sorting with USORT()
Ampersand and Passing by Reference
Occasionally, within function arguments, a variable will be listed with a preceeding ampersand, "&". This specifies the variable as being only a reference to the original (with any changes made to the variable inside the scope of the function being applied to the original value outside the function). In this fashion, the modified variable need not be returned or globalized to see changes made by the function. The function simply refers to the original value, and changes are applied there, all in one shot.
Conversion Specification Type Codes
The following designations are used inside of PRINTF and SPRINTF to indicate the format of the variable displayed:
ECHO, PRINT, PRINTF, and SPRINTF Differences
While both ECHO and PRINT output to the browser, PRINT is a function and provides a return value (0 or 1 depending on whether the function was successful or not). PRINTF and SPRINTF both use Conversion Specification Type Codes. Like ECHO, PRINTF also outputs to the browser, while SPRINTF returns output like PRINT. Both of these functions take an argument for a string to be formatted, followed by a list of arguments specified for placement inside the string (at locations designated by conversion specification codes within the string). The conversion spec format begins with a "%", followed by an optional apostrophe and padding character, followed by an optional dash, "-", followed by optional width (in characters), followed by a precision indicator (decimal followed by the number of places to display after the decimal), followed by the Type Code.
ISSET() vs. EMPTY()
Often, a conditional will be coded such that verifies both that the variable contains a value (ie. not NULL) and that the value is not "0" or blank. To combine both tests into one check, use instead the EMPTY function which checks for actual data in one shot.
PHP Image Functions
Regular Expressions (REGEX)
Most commonly used in the EREG_REPLACE and EREGI_REPLACE functions, regular expressions find patterns in data. The pattern is determined by the contents of the regular expression. Any part of the expression that is enclosed with square brackets ("[" and "]") will be used as the comparison value. The following characters specify patterns outside the comparison value:
REQUIRE() vs INCLUDE()
INCLUDE is run every time the statement is executed, but REQUIRE will run only once, regardless of whether or not the statement is executed (thus making it pointless to nest REQUIRE in a conditional). Included files (using INCLUDE only) may also use a return value (the same as a function). However, due to the more simplistic way that REQUIRE works, it does execute faster than INCLUDE.
User Specified Array Sorting with USORT()
Should a developer require a method to sort an array in a non-standard fashion, USORT comes in very handy. It takes two arguments: the array to be sorted, and the name of the function used to sort. This function is typically defined by the developer so that it takes two arguments, current and next array value. The return code instructs USORT as to whether the two values passed are equal (0), or whether the first value should come before or after the comparison value (1 or -1, respectively).