print_r
¶print_r
is a function that recursively prints any object passed to ittrue
to force print_r
to return a string rather than printing directlyprint_r(explode(',', "John,Paul,George,Ringo"));
print_r(explode(',', "John,Paul,George,Ringo"),true);
array
function$an_array = array(1,2,3,"Hello")
key => value
$assoc_array = array('course'=>'CMSC 433', 'title'=>'Scripting Languages');
$assoc_array = array('course'=>'CMSC 433',
'title'=>'Scripting Languages',
'time'=> 11.5);
echo print_r($assoc_array,true);
$an_array[] = 5;
$an_array['new_key'] = 10;
$some_numbers = array(0,4,6,1,4,9,0,22);
$some_numbers[] = 100;
echo print_r($some_numbers,true)
$ascii = array('A' => 65, 'B' => 66, 'a' => 97);
$ascii['b'] = 98;
echo print_r($ascii,true);
$ascii = array('A' => 65, 'B' => 66, 'a' => 97);
$ascii[] = 98;
echo print_r($ascii,true);
echo print_r(range(1,10) ,true);
## Range in PHP must take two parameters!
echo print_r(range(10),true);
echo print_r(range(1,10,4),true);
count(range(1,10));
$info = array('coffee', 'brown', 'caffeine');
list($drink, $color, $power) = $info;
echo $drink;
echo $color;
echo $power;
$my_range = range(1,10);
echo print_r(array_slice($my_range,3,7),true);
echo print_r(array_slice($my_range,-4,6),true);
echo print_r(array_chunk($my_range,2),true);
array_key_exists('Dog',range(0,10));
array_key_exists('Dog',array('Dog' => 40, 'Cat' => 20))
in_array(10,range(0,10));
in_array(10,range(0,5));
in_array(10,array('ten' => 10, 'twenty' => 20));
in_array(0,array('ten' => 10, 'twenty' => 20));
shuffle(range(1,10));
shuffle($my_range);
echo print_r($my_range,true);
array_sum(array(1,2,3,"5",2.4));
extract
and compact
are two functions that allow you to convert from and to arrays respecitivelyextract
creates new variables with names based on the keys in the arraycompact
on the other hand takes a list of strings, and looks for variables with those names, creating an array out of them$info = array('school'=>'UMBC',
'department'=>'CSEE',
'building'=>'ITE');
extract($info);
echo print_r($info,true);
echo "The $department department of $school is located in $building";
$name = 'Raúl González Blanco';
$position = "Forward";
$nationality = "Spanish";
$player = compact('name','position','nationality');
echo print_r($player,true);
$matrix[0][] = 10;
$matrix[1][1] = 20;
$matrix[1]['Key'] = 30;
$matrix[1][] = 3;
$matrix[][][] = 40;
#shift($matrix,10);
echo print_r($matrix,true);
foreach
constructforeach($array as $element){
}
$i = 10;
while($i > 0)
{
echo $i;
$i--;
}
$i = 10;
do
{
echo $i;
$i--;
}while($i > 0); #Note the Semicolon
for($i = 0; $i < 10; $i++){
echo $i;
}
$to_loop = array('a', 'b', 'c', 'd', 'e');
foreach($to_loop as $el){
echo $el;
}
$to_loop = array('a'=>97, 'b'=>98, 'c'=>99, 'd'=>100, 'e'=>101);
foreach($to_loop as $el){
echo $el;
}
$to_loop = array('a'=>97, 'b'=>98, 'c'=>99, 'd'=>100, 'e'=>101);
foreach($to_loop as $k => $el){
echo $k, $el;
}
function
$
notationfunction my_first_function(){
echo "Hello from inside a function";
}
my_first_function();
my_first_function();
function someMath($number1, $number2){
echo $number1 * $number2;
}
someMath(1,2);
function someMath2($number1, $number2){
return $number1 * $number2;
}
someMath2(1,2);
global
static
function scope_tester() {
$a .= " World!";
echo "a in scope_tester(): $a";
}
$a = "Hello";
echo "a before scope_tester(): ", $a;
scope_tester();
echo "a after scope_tester(): ", $a;
function scope_tester2() {
$a .= " World!";
echo "a in scope_tester(): $a";
}
global $a;
$a = "Hello";
echo "a before scope_tester2(): ", $a;
scope_tester2();
echo "a after scope_tester2(): ", $a;
function scope_tester3() {
global $a;
$a .= " World!";
echo "a in scope_tester(): $a";
}
$a = "Hello";
echo "a before scope_tester3(): ", $a;
scope_tester3();
echo "a after scope_tester3(): ", $a;
function counter() {
static $n = 0;
$n++;
return $n;
}
echo counter();
echo counter();
echo counter();
&
before the variable in the definitionfunction change_array($arr){
$arr[0] = 20;
echo $arr[0];
}
$my_array = range(0,10);
echo $my_array[0];
change_array($my_array);
echo $my_array[0];
function change_array_correct(&$arr){
$arr[0] = 20;
echo $arr[0];
}
$my_array = range(0,10);
echo $my_array[0];
change_array_correct($my_array);
echo $my_array[0];
change_array_correct(range(0,10));
function say_hello($who = "World"){
echo "Hello $who!";
}
say_hello();
say_hello("Class");
function custom_range($end, $start = 0){
return range($start,$end);
}
echo print_r(custom_range(4),true);
echo print_r(custom_range(4,1),true);
&
) before the function name in the declarationfunction &name(....)
{
}
function getFredReturnByValue() {
global $names;
return $names[0];
}
function & getFredReturnByRef() {
global $names;
return $names[0];
}
$names = array("Fred", "Barney", "Wilma", "Betty");
$fred = getFredReturnByValue();
echo $fred;
$fred = 'Bam Bam';
$fred = getFredReturnByValue();
echo $fred;
$fred =& getFredReturnByRef();
echo $fred;
$fred = 'Bam Bam';
$fred =& getFredReturnByRef();
echo $fred;
func_get_args
function maximum() {
$args = func_get_args();
$max = $args[0];
for($i = 1, $n = count($args); $i < $n; $i++) {
$max = $args[$i] > $max ? $args[$i] : $max;
}
return $max;
}
echo maximum(1,2,300,4,5,6);
function
keyword$max = function($a,$b){
return ($a > $b)?$a:$b;
}; #Note the semicolon
$max(1,2);
fopen(file_name)
file_get_contents(name)
file(name)
if($fp = fopen('hello.sh','r')){
while($line = fgets($fp)){
echo htmlspecialchars($line);
}
}else{
echo "Can't read file";
}
htmlspecialchars(file_get_contents('hello.sh'));
$contents = file('hello.sh');
echo print_r($contents,true);
file_put_contents
(Since PHP 5 on)if($fp = fopen('hello.txt','w')){
fwrite($fp, "Hello");
fwrite($fp, " World\n");
fwrite($fp, "You need to include new lines explicitly\n");
fclose($fp);
}else{
echo "Can't open file";
}
if($fp = fopen('hello.txt','a')){
fwrite($fp, "Hello");
fwrite($fp, " World\n");
fwrite($fp, "You need to include new lines explicitly\n");
fclose($fp);
}else{
echo "Can't open file";
}
file_put_contents("hello.txt","This\nWrites\nIt\nAll at once!")
file_put_contents("hello.txt","This\nWrites\nIt\nAll at once!",FILE_APPEND)
file_put_contents("range.txt",range(0,20));
if($fp = fopen('hello.txt','a')){
if(flock($fp,LOCK_EX)){
fwrite($fp, "Hello");
fwrite($fp, " World\n");
fwrite($fp, "You need to include new lines explicitly\n");
flock($fp,LOCK_UN);
fclose($fp); }
else{
echo "Couldn't get lock";
}
}else{
echo "Can't open file";
}
file_put_contents("range.txt",range(0,20),LOCK_EX);
include(file_name)
- Includes and evaluates the filerequire(file_name)
- Includes and evaluates the file, killing the script if unable to do soinclude_once(file_name)
- Only include and evaluate this file one timerequire_once(file_name)
- Only require this file one timeinclude 'header.php'; #same as include('header.php')
include 'I dont exist';
require 'I dont exist';
include_once 'header.php';
include_once 'header.php';