PHP Notes and Examples: Difference between revisions
Jump to navigation
Jump to search
mNo edit summary |
mNo edit summary |
||
Line 52: | Line 52: | ||
lib.inc | lib.inc | ||
</pre> | </pre> | ||
Go BACKWARDS in array | |||
From: [https://stackoverflow.com/questions/2194388/php-how-to-get-the-element-before-the-last-element-from-an-array php-how-to-get-the-element-before-the-last-element-from-an-array] | |||
<pre> | |||
<?php | |||
$foo=["one","two","three","four"]; | |||
echo "TEST " . $foo[count($foo)-2]; | |||
?> | |||
Returns TEST three | |||
</pre> | |||
====Slim4 Framework Notes==== | ====Slim4 Framework Notes==== |
Revision as of 15:19, 19 June 2023
PHP Notes and Examples
change Array to JSON
$foo=json_encode($array,1); echo $foo . "\n";
Convert a VALID JSON string to array
$foo=json_decode($json, True); print_r($foo);
Example foreach loop (simple)
foreach ($nestedArray1 as $Array1) { do something with new array $array1[??}; }
Example Try Catch block
Use Exception; try { someRandoFunction; } catch (Exception $e) { doSomethingWhenErrorFound; } catch (ErrorName $e) { cryToMamma; }
Checking if file or dir exists. Starting point:
<?php $path_parts = pathinfo('/www/htdocs/inc/lib.inc.php'); echo $path_parts['dirname'], "\n"; echo $path_parts['basename'], "\n"; echo $path_parts['extension'], "\n"; echo $path_parts['filename'], "\n"; ?> The above example will output: /www/htdocs/inc lib.inc.php php lib.inc
Go BACKWARDS in array From: php-how-to-get-the-element-before-the-last-element-from-an-array
<?php $foo=["one","two","three","four"]; echo "TEST " . $foo[count($foo)-2]; ?> Returns TEST three
Slim4 Framework Notes
return $this->respondWithData($data); throw new HttpBadRequestException($this->request, "Error message details");