<?php
$init="0";
$num=0;
while($num<3) {
$init.="0";
$num++;
echo $init."<br>";
}
echo $init;
?>
00
000
0000
0000
lunes, 27 de febrero de 2017
lunes, 13 de febrero de 2017
Error reporting
<?php
// Turn off all error reportingerror_reporting(0);
// Report simple running errorserror_reporting(E_ERROR | E_WARNING | E_PARSE);
// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
// Report all errors except E_NOTICEerror_reporting(E_ALL & ~E_NOTICE);
// Report all PHP errors (see changelog)error_reporting(E_ALL);
// Report all PHP errorserror_reporting(-1);
// Same as error_reporting(E_ALL);ini_set('error_reporting', E_ALL);
?>
// Turn off all error reportingerror_reporting(0);
// Report simple running errorserror_reporting(E_ERROR | E_WARNING | E_PARSE);
// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
// Report all errors except E_NOTICEerror_reporting(E_ALL & ~E_NOTICE);
// Report all PHP errors (see changelog)error_reporting(E_ALL);
// Report all PHP errorserror_reporting(-1);
// Same as error_reporting(E_ALL);ini_set('error_reporting', E_ALL);
?>
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
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];
?>
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
)
$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
$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
$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>";
}
?>
<?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>";
}
?>
Suscribirse a:
Entradas (Atom)