Difference between revisions of "PHP"

From wiki
Jump to: navigation, search
(Functions)
 
(One intermediate revision by the same user not shown)
Line 28: Line 28:
 
:Current timestamp (YYYY-mm-dd HH:MM:SS)
 
:Current timestamp (YYYY-mm-dd HH:MM:SS)
  
;strpos($string, $search)
+
;strpos($string, $search,$start)
:Return position of $search exists in $string if it is not in return FALSE
+
:Return position of $search exists in $string. When it is not in, return FALSE. Start searching at position $start (optional). Simple test on strpos - <code>if (strpos(x,y)) {}</code> - does not differ between position 0 and FALSE. Test on position 2 or count from the end by setting $start to a negative value (possible since PHP7.1).
 
;preg_match($pattern, $string)
 
;preg_match($pattern, $string)
 
:Return 1 if $pattern exists in $string
 
:Return 1 if $pattern exists in $string
Line 62: Line 62:
 
:Return the number of elements (length) of an array
 
:Return the number of elements (length) of an array
  
;foreach($arr1 as $key => $value) { }
+
;foreach($arr1 as $value) { codeblock }
 +
:Loop over an indexed array
 +
 
 +
;foreach($arr1 as $key => $value) { codeblock }
 
:Loop over an associative array
 
:Loop over an associative array
  

Latest revision as of 11:11, 1 August 2020

Script language for dynamic web-pages. The syntax and concepts used are similar to Perl

Loop over an associative array (hash):

 
foreach ( $_POST as $key => $value ) {
            echo $key.' '.$value.' '.$_POST[$key].'<br>';
        }

Limited number of loops:

 
for ($i = 0; $i <= 6; $i++) {
  echo "Loop number: $i ";
}
escapeshellcmd(shellcommand)
Return the shellcommand with everything that might confuse the shell escaped.
exec(<cmd>,<outputarray>,<returnvar>)
Executed <cmd> in the shell, append the command output to he array <outputarray> and set <returnvar> to the returnvalue of the command.
Also the last line of the command output is returned
shell_exec(<cmd>)
Execute <cmd> (output from excapedshellcmd) in the shell, return the command output.
date("Y-m-d h:i:s")
Current timestamp (YYYY-mm-dd HH:MM:SS)
strpos($string, $search,$start)
Return position of $search exists in $string. When it is not in, return FALSE. Start searching at position $start (optional). Simple test on strpos - if (strpos(x,y)) {} - does not differ between position 0 and FALSE. Test on position 2 or count from the end by setting $start to a negative value (possible since PHP7.1).
preg_match($pattern, $string)
Return 1 if $pattern exists in $string
str_replace($old,$new,$string)
Replace $old with $new in $string

Functions

All variables are local to the function. You have to pass everything it needs.

<?php
function func1($arg1, $arg2)
{
    <codeblock>
    return $value;
}
?>

Arrays

arr1 = ( val1, val2, val3 )
Create an indexed array. The keys are the range 0..arraylength.
$arr1 = array(key1=>val1, key2=>val2, key3=>val3)
$arr2[key1] = val1
Create an associative array (like a hash in perl)
count(arr1)
Return the number of elements (length) of an array
foreach($arr1 as $value) { codeblock }
Loop over an indexed array
foreach($arr1 as $key => $value) { codeblock }
Loop over an associative array

Install

On debian (and its derivates)

apt-get install php-fpm php-mysql
Install php7 and mysql support
Fix php security by editing /etc/php/7.0/fpm/php.ini
Uncomment line with cgi.fix_pathinfo and set it to 0