To get a html page to display UTF-8 encoded text correctly (without setting a default charset) simply add the following to the head of the html page.
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
To get a html page to display UTF-8 encoded text correctly (without setting a default charset) simply add the following to the head of the html page.
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
The first idea that comes into mind for doing this is to simply make a typecast of the boolean variable to a string and use that (which works)
$testVar = false; echo (string)$testVar; //will output a empty string (as that evaluates to false) $testVar = true; echo (string)$testVar; //will output 1 (as that evaluates to true)
but what if the requirement is that the string should say true or false
$testVar = false; echo $testVar ? 'true' : 'false'; //will output false $testVar = true; echo $testVar ? 'true' : 'false'; //will output true
(While the above example might seam useless I recently had to do this for a db insert)
Sometimes it is nice to know how long a scrip took to execute (or a part of a script).
This is a simple way to time a script in php.
$time_start = microtime(true); usleep(500); $time_end = microtime(true); $time = $time_end - $time_start; echo "run time was " . $time . " seconds";
Very quick (&dirty):
1 2 3 4 5 6 7 8 | try{ doStuff(); } catch (Exception $e){ echo 'Error on line '. $e->getLine().' in '. $e->getFile() . $e->getMessage(); //Or if you are working inside an object, you could use. echo 'Error on line '.$this->getLine().' in '.$this->getFile() . $e->getMessage(); } |
Now to catch a specific kind of exception (in this case CustomException), and say woups, but to re-throw all other Exceptions.
1 2 3 4 5 6 7 8 9 10 11 | try{ doStuffThatCanGiveCustomException(); } catch (Exception $e){ //echo get_class($e) . "<br/>"; if (get_class($e) == "CustomException" && $e->getMessage() === "My custom exception message."){ echo "woups"; } else throw $e; } |
To throw a custom exception:
throw new Exception('My exception message');
For more informaion about this please check out the php manual
If you do not wish to change character encoding for the entire server, but only one site (or only one directory) then this is possible to do using .htaccess.
AddDefaultCharset UTF-8
If you only wish to do this to php files (and not all files)
AddDefaultCharset UTF-8 .php
If you wish to modify the mime-type as well as the encoding (on html files in this example)
AddType 'text/html; charset=UTF-8' html