viernes, 13 de enero de 2017

php function return true false

<?php

function val($a) {


if($a>=1){

return true;

}
else  {
return false;
}
}


$d ;

?>
var_dump(val($d));  //faslse


$d=1;

var_dump(val($d));  //true

miércoles, 11 de enero de 2017

reading files with fread and fgets

<?php

$file=fopen("/root/list.txt","r");
$arr=array();
while(!feof($file)){

$list=fgets($file);

$arr[]=$list;

}

fclose($file);
echo $arr[0];
?>
this will print  11111 ( first line of are list file)





<?php

$file=fopen("/root/list.txt","r");
$arr=array();
while(!feof($file)){

$list=fread($file,9999);

$arr[]=$list;

}

fclose($file);
echo $arr[0];
?>
this  print all values on  the file because  the amount of data is smaller than 9999

11111
22222
33333
44444


If we use fgets with no parameter   each line will be a different variable.

if we use fread all values  will be one variable if  are smaller than the number of byte specif on the parameter


Also using  File()

<?php

print_r(file("list.txt",FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));

?>

Array
(
    [0] => 101
    [1] => 102
    [2] => 104
    [3] => 104
)

viernes, 6 de enero de 2017

Looping in a multi dimensional array

<?php
$records = array(
    array(
        'id' => 2135,
        'first_name' => 'John',
        'last_name' => 'Doe',
    ),
    array(
        'id' => 3245,
        'first_name' => 'Sally',
        'last_name' => 'Smith',
    ),
    array(
        'id' => 5342,
        'first_name' => 'Jane',
        'last_name' => 'Jones',
    ),
    array(
        'id' => 5623,
        'first_name' => 'Peter',
        'last_name' => 'Doe',
    )
);


foreach($records as $key=> $value){

foreach($records[$key] as $value ){

echo " $value <br>";

}


}

?>

2135
John
Doe
3245
Sally
Smith
5342
Jane
Jones
5623
Peter
Doe

More advanced method

<?php
$records = array(
    array(
        'id' => 2135,
        'first_name' => 'John',
        'last_name' => 'Doe',
    ),
    array(
        'id' => 3245,
        'first_name' => 'Sally',
        'last_name' => 'Smith',
    ),
    array(
        'id' => 5342,
        'first_name' => 'Jane',
        'last_name' => 'Jones',
    ),
    array(
        'id' => 5623,
        'first_name' => 'Peter',
        'last_name' => 'Doe',
    )
);


foreach($records as $key=> $value){


echo " Array # $key<br>";

foreach($records[$key] as $value){

echo " $value<br>";


}


}
 ?>

Array # 0
2135
John
Doe
Array # 1
3245
Sally
Smith
Array # 2
5342
Jane
Jones
Array # 3
5623
Peter
Doe

Other  conditional method

<?php
$records = array(
    array(
        'id' => 2135,
        'first_name' => 'John',
        'last_name' => 'Doe',
    ),
    array(
        'id' => 3245,
        'first_name' => 'Sally',
        'last_name' => 'Smith',
    ),
    array(
        'id' => 5342,
        'first_name' => 'Jane',
        'last_name' => 'Jones',
    ),
    array(
        'id' => 5623,
        'first_name' => 'Peter',
        'last_name' => 'Doe',
    )
);


foreach($records as $key=> $value){


if($key==3){

foreach($records[$key] as $value){

echo " $value<br>";

}
}


}
?>

5623
Peter
Doe

Displaying only ID key

<?php
$records = array(
    array(
        'id' => 2135,
        'first_name' => 'John',
        'last_name' => 'Doe',
    ),
    array(
        'id' => 3245,
        'first_name' => 'Sally',
        'last_name' => 'Smith',
    ),
    array(
        'id' => 5342,
        'first_name' => 'Jane',
        'last_name' => 'Jones',
    ),
    array(
        'id' => 5623,
        'first_name' => 'Peter',
        'last_name' => 'Doe',
    )
);


foreach($records as $key=> $value){




foreach($records[$key] as $key=> $value){


if($key=="id"){
echo " $value<br>";


}

}


}
?>

2135
3245
5342
5623

Multi dimensional array

<?php
$records = array(
    array(
        'id' => 2135,
        'first_name' => 'John',
        'last_name' => 'Doe',
    ),
    array(
        'id' => 3245,
        'first_name' => 'Sally',
        'last_name' => 'Smith',
    ),
    array(
        'id' => 5342,
        'first_name' => 'Jane',
        'last_name' => 'Jones',
    ),
    array(
        'id' => 5623,
        'first_name' => 'Peter',
        'last_name' => 'Doe',
    )
);
?>




echo $records[0]['id']."<br>";
echo $records[1]['id']."<br>";
echo $records[2]['id']."<br>";


2135
3245
5342

lunes, 2 de enero de 2017

printing random keys and values from an array

array_rand() print ramdom keys of an array into another array, let  print the values using those keys

<?php

$arr=array(100,222,11,333,994,8844,13,113,913);

$rand=array_rand($arr,3);

foreach($rand as $key) {

echo $arr[$key]."<br>";

}

?>