code • PHP - martindubenet/wed-dev-design GitHub Wiki
Very basic PHP stuff that I keep on forgetting after a while...
Original (long) | Shorthand |
---|---|
<?php echo( $my_var ); ?>
|
<?= $my_var ?>
|
- Composer : Same as npmJs but for PHP.
- scssphp : SCSS compiler written in PHP. ➥ Github clone
Return and start on a new line. (this requires double quotes characters)
echo "\r\n";
See logical and comparaison Operators
This is fundamentally a negative check so in order to use it you need to add the negation « !
» as prefex to make it positive.
In the following example
<?php
if (!empty( $var1 )) {
echo '<code>' . $var2 . '</code>';
}
?>
<?php
if ( $var1 == false || (!isset( $var1 )) ) {
echo '<code>' . $var2 . '</code>';
}
?>
This works fine for a simple single level of data
$jsonFile = utf8_encode( file_get_contents( "../any_path/file.json" ) );
// Decode JSON data as a PHP associative array
$phpArray = json_decode($jsonFile, true);
// Loop through the associative array of a multi-level Json data node
foreach( $phpArray as $loopedItem ) {
echo '<dt>'
.$phpArray["unlooping_jsonNode"].
'</dt><dd>'
.$loopedItem["parentContainer_jsonNode"]["childData_jsonNode"].
'</dt>';
}
Because when developping with MAMP or WAMP local servers we sometimes forget to reset the "OTHER" server... It's alway useful to have those at hand.
$this_server_ip = $_SERVER['REMOTE_ADDR'];
$localhost_ip = "127.0.0.1";
if ( ( $this_server_ip == $localhost_ip ) || (empty( $rootPath )) ) {
$rootPath = "http://localhost/";
} else {
$rootPath = "/";
}
dirname(__FILE__)
<?php require_once(dirname(__FILE__) . '/inc/variables.php'); ?>
or
$projectSlug = '/vrak2014/';
$serverRoot = $_SERVER['DOCUMENT_ROOT'];
$rootPath = $serverRoot.$projectSlug;