Sitemap NEW: Blickling Hall   |   Haven

PHP Snippets and Code Examples
PRIVATE CARAVAN RENTAL AT CAISTER–ON–SEA, NORFOLK. Sat Nav: NR30 5NH


About these Help Pages
These Help pages are provided to assist staff and contributors to caistercaravan.co.uk. They are also available to anyone else who wishes to use them at their own risk. All help pages have been checked and found to work perfectly well for us.

PHP Function htmlentities()

Using the PHP function htmlentities() is a way of printing or displaying actual HTML or PHP code on the screen without it being processed by a browser or the server. It is frequently used to display or print code examples.

EXAMPLE

If you want to display a paragraph with HTML tags saying 'Hello world' use:
(Copy only the red code)
$ram = '<p>Hello world</p>';
$ram = htmlentities($ram);
echo $ram;

This is the result
<p>Hello world</p>

PHP IF Statement - Using Multiple Conditions

&& means AND

|| means OR

= means an assignment

== means a comparison

EXAMPLE

$bookType = "History";

$author = "M Knight";

if ($bookType == "History" && $author == "M Knight") {echo "The $bookType book was written by $author";}

The History book was written by M Knight.

EXAMPLE

$Number = "Two";

if (($Number == "One") || ($Number == "Two") || ($Number == "Three")) {echo "Yes, $Number exists.";}

Yes, Two exists.


Format a Number as Currency (GBP)

EXAMPLE

$value = "75.2";

$value = sprintf("%01.2f",$value);

echo "Result is:" . htmlentities(£) . "$value";

Result is: £75.20


Format a Number with Leading Zeros

This example has a padding of zeros to make up a total of 8 numbers

EXAMPLE

$input = "123";

$input = str_pad($input, 8, '0', STR_PAD_LEFT);

echo "The padded number is: $input";

Result is: The padded number is: 00000123


Limit the Printed Length of Text or Data

Use the function substr() where the first number is the last to be omitted and the second is the number of following characters to print.

EXAMPLE

$text = "Expectations";

$text = substr($text,0,6);

echo $text;

Result is: Expect

EXAMPLE 2

$text = "Expectations";

$text = substr($text,6,3);

echo $text;

Result is: ati


Back to Help Index