Matt Fiddles

Life's so vast, there's just so much to do...

User Tools

Site Tools


Sidebar

"And finally, " said Max, quieting the audience down and putting on his solemn face, "finally I believe we have with us here tonight, a party of believers, very devout
believers, from the Church of the Second Coming of the Great Prophet Zarquon. " ... "There they are, " said Max, "sitting there, patiently. He said he'd come again, and
he's kept you waiting a long time, so let's hope he's hurrying fellas, because he's only got eight minutes left! "



Where will you go today?

"Ohhh! Great warrior!" [laughs and shakes his head]
"Wars not make one great!"

- Yoda--The Empire Strikes Back
computers:websites:php-notes

PHP Notes

Variables

List of main variables: https://php.net/manual/en/reserved.variables.server.php

$_SERVER['DOCUMENT_ROOT'] //The server's path that matches www.example.com/

List of constants: https://php.net/manual/en/language.constants.predefined.php

__FILE__  //The path of the current script.
__DIR__   //The directory of the current script.

Dump vars

Use print_r() and var_dump() to show what vars are holding.

And to show what a class has:

$class_methods = get_class_methods('myclass');
// or
$class_methods = get_class_methods(new myclass());
foreach ($class_methods as $method_name) 
{
    echo "$method_name<br/>";
}

Arrays

To see if an array is empty: if (empty($errors)) { code; } <code php> array_key_exists($key, $array); in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] ); array_search ( mixed $needle , array $haystack [, bool $strict = false ] ); preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] ) preg_grep ( string $pattern , array $input [, int $flags = 0 ] ) </code> Use unset() on a loop to remove specific arrays. ===== Functions ===== ==== Date ==== Set the timezone. date_default_timezone_set('America/Los_Angeles'); Some examples $date = date('Y-m-d H:i:s'); 2012-03-06 17:33:07

$today = date("F j, Y, g:i a");     // March 10, 2001, 5:16 pm

A full list of all date strings: https://php.net/manual/en/function.date.php

And in OOP

$now = new DateTime(null, new DateTimeZone('America/New_York'));
// $now->setTimezone(new DateTimeZone('Europe/London'));    // Another way
echo $now->getTimezone();

$date = new DateTime('now', new DateTimeZone('America/New_York'));
echo $date->format('d-m-Y H:i:s');

Don't show errors

First. Fix the errors. Error prone code is slow and hurts the site.

But sometimes it's needed. Like using file_get_contents() on a remote site:

Warning: file_get_contents(example.com) [function.file-get-contents]: failed to open stream:

That's annoying. Instead use @ on the function to silent it:

$output=@file_get_contents("http://example.com");
// $output is the webpage, fyi
if ($output) {
  echo "The site is up!";
}
else {
  echo "Um... Houston, we have a problem.";
}

Ref: http://eureka.ykyuen.info/2011/12/07/php-suppress-and-catch-file_get_contents-warning/

PHP Error Pages

Serialized Data

unserialize($data);
serialize($data);

Comparison

$a == $b 	Equal 	        TRUE if $a is equal to $b after type juggling.
$a === $b 	Identical 	TRUE if $a is equal to $b, and they are of the same type.
$a != $b 	Not equal 	TRUE if $a is not equal to $b after type juggling.
$a <> $b 	Not equal 	TRUE if $a is not equal to $b after type juggling.
$a !== $b 	Not identical 	TRUE if $a is not equal to $b, or they are not of the same type.
$a < $b 	Less than 	TRUE if $a is strictly less than $b.
$a > $b 	Greater than 	TRUE if $a is strictly greater than $b.
$a <= $b 	Less than or equal to 	TRUE if $a is less than or equal to $b.
$a >= $b 	Greater than or equal to 	TRUE if $a is greater than or equal to $b.

http://viralpatel.net/blogs/15-very-useful-php-code-snippets-for-php-developers/

Other pages

computers/websites/php-notes.txt · Last modified: Nov 2, 2015 (3 years ago) by Matt Bagley