Tag Archives: PHP

Class Constants

It is possible to define constant values on a per-class basis remaining the same and unchangeable. Constants differ from normal variables in that you don’t use the $ symbol to declare or use them. The default visibility of class constants is public.

The value must be a constant expression, not (for example) a variable, a property, or a function call.

It’s also possible for interfaces to have constants. Look at the interface documentation for examples.

As of PHP 5.3.0, it’s possible to reference the class using a variable. The variable’s value can not be a keyword (e.g. self, parent and static).

Note that class constants are allocated once per class, and not for each class instance.

  • Class constants are public
  • Class constants are being inherited
  • Class constants can be initialized by const
<?php
 class MyClass {
 const CONSTANT = 'constant value';

function showConstant() {
 echo self::CONSTANT . "\n";
 }
 }
 
 echo MyClass::CONSTANT . "\n";
 
 $class = new MyClass();
 $class->showConstant();
 
?>

Unbuffered Queries

Unbuffered MySQL queries execute the query and then return a resource while the data is still waiting on the MySQL server for being fetched. This uses less memory on the PHP-side, but can increase the load on the server. Unless the full result set was fetched from the server no further queries can be sent over the same connection. Unbuffered queries can also be referred to as “use result”.

func_get_args

Returns an array in which each element is a copy of the corresponding member of the current user-defined function’s argument list.

<?php
 function testFunction() {
 echo "Number of arguments : " .func_num_args();
 }
testFunction(1, 2, 3);
?>

It outputs Number of arguments : 3

ob_start

This function will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer.

The contents of this internal buffer may be copied into a string variable using ob_get_contents(). To output what is stored in the internal buffer, use ob_end_flush(). Alternatively, ob_end_clean() will silently discard the buffer contents.

<?php
 function callback($buffer) {
 return (str_replace("PHP", "PHP Codez", $buffer));
 }
 ob_start("callback");
?>
 <p>Welcome to PHP</p>
<?php
 ob_end_flush();
?>

The output will be Welcome to PHP Codez

quote_meta

The quotemeta() function adds backslashes in front of some predefined characters in a string.

This function can be used to escape characters with special meanings, such as ( ), [ ], and * in SQL.

This function is binary-safe.

The predefined characters are:

  • period (.)
  • backslash (\)
  • plus sign (+)
  • asterisk (*)
  • question mark (?)
  • brackets ([])
  • caret (^)c
  • dollar sign ($)
  • parenthesis (())

Example

<?php
 $str = "PHP Codez. (Welcome)";
 echo quotemeta($str);
?>

It outputs PHP Codez\. \(Welcome\)

header_remove

It is used to remove previously set headers

<?php
 header("X-Foo: Bar");
 header("X-Bar: Baz");

echo "<pre>";print_r(headers_list());

header_remove("X-Foo"); 
 echo "<pre>";print_r(headers_list());
?>

Output

Array
(
    [0] => X-Powered-By: PHP/7.1.14
    [1] => X-Foo: Bar
    [2] => X-Bar: Baz
)Array
(
    [0] => X-Powered-By: PHP/7.1.14
    [1] => X-Bar: Baz
)

parse_url

This function parses a URL and returns an associative array containing any of the various components of the URL that are present.

  • scheme – e.g. http
  • host
  • port
  • user
  • pass
  • path
  • query – after the question mark ?
  • fragment – after the hashmark #

This function is not meant to validate the given URL, it only breaks it up into the above listed parts. Partial URLs are also accepted, parse_url() tries its best to parse them correctly.

<?php
 $url = 'phpcodez.com/index.php?user=1';
 echo "<pre>";print_r(parse_url($url)); 
?>
Output
Array
(
    [path] => phpcodez.com/index.php
    [query] => user=1
)

SimpleXML

SimpleXML is an extension that allows us to easily manipulate and get XML data.

SimpleXML provides the ability to iterate over items in an XML document, as well as
access items within it as if they were object properties

SimpleXML provides an easy way of getting an element’s name, attributes and textual content if you know the XML document’s structure or layout.

SimpleXML turns an XML document into a data structure you can iterate through like a collection of arrays and objects.

  • SimpleXML allows removal of attributes.
  • SimpleXML allows addition of new attributes.
  • SimpleXML allows removal of nodes.
  • SimpleXML allows addition of new nodes.