Browsing Tag

tricks

WordPress Tutorials

PHP tricks

December 19, 2017

Using @ to disable errors

If you find an error message particularly annoying and you are sure it definitely does not apply to you, PHP has a method for you to silence the message entirely. If you place an at symbol, @, before a function that generates an error, PHP will catch the error and silence it entirely. Consider the following two complete scripts:

Script 1

<?php
cactus();
?>

Script 2

<?php
@cactus();
?>

In script one, the undefined function cactus() is called. When this script is run, PHP will halt execution at the undefined function, and print out a fatal error message. However, when the second script is run, PHP will attempt to call the cactus() function, fail, but will not halt execution or print out an error – all thanks to the @ symbol before the function call.

While there are legitimate uses for suppressing errors in this way, it is not advised, because it usually works in the same way that sweeping dust under a carpet does not make a house any cleaner – hiding a problem is not the same as solving it! If you explicitly wish to have errors suppressed with @, it is strongly advised that you always write your own code to check return values of functions.

Removing HTML from a string

string strip_tags ( string source [, string allowable_tags])

strip_tags() is a function that allows you to strip out all HTML and PHP tags from a given string (parameter one), however you can also use parameter two to specify a list of HTML tags you want.

This function can be very helpful if you ever display user input on your site. For example, if you create your own messageboard forum on your site a user could post a title along the lines of: <h1>HELLO WORLD!</h1>, which, because you would display the titles of each post on your board, would display their unwanted message in huge letters on your visitors’ screens.

Here are two examples of stripping out tags:

<?php

$input = “<i><h1>Hello World!</h1></i>”;
$a = strip_tags($input);
$b = strip_tags($input, “<h1><b>”);

?>

After running that script, $a will be set to “Hello World!”, whereas $b will be set to “<h1>Hello World!</h1>” because we had “<h1>” in the list of acceptable tags.

PHP Output Buffering

bool ob_start ( [callback output_function])
bool ob_end_flush ( void )
bool ob_end_clean ( void )

There are two ways to start buffering output: through a setting in php.ini to enable output buffering for all scripts, or by using a function call on a script-by-script basis. Surprisingly, the latter is preferred – it makes your code more portable, and also gives you greater flexibility.

To create a new output buffer and start writing to it, call ob_start(). There are two ways to end a buffer, which are ob_end_flush() and ob_end_clean() – the former ends the buffer and sends all data to output, and the latter ends the buffer without sending it to output. Every piece of text outputted while an output buffer is open is placed into that buffer as opposed to being sent to output. Consider the following script:

<?php
ob_start();
print “Hello First!\n”;
ob_end_flush();

ob_start();
print “Hello Second!\n”;
ob_end_clean();

ob_start();
print “Hello Third!\n”;
?>


That script will output “Hello First” because the first text is placed into a buffer then flushed with ob_end_flush().
The “Hello Second” will not be printed out, though, because it is placed into a buffer which is cleaned using ob_end_clean() and not sent to output.
Finally, the script will print out “Hello Third” because PHP automatically flushes open output buffers when it reaches the end of a script.