📜 ⬆️ ⬇️

Using expressions in PHP

PHP. , . .

— PHP. , PHP, . — « , ». ( php.net)

UPD: , , . ( ) , . — , .


80-90% PHP- :

$a = 2 + 1;
$a = isset($a) ? $a : false;
$a = func1($a) + func2($a);
$res = mysql_query($query);


. PHP:

2 + 1;
isset($a) ? $a : false;
func1($a) + func2($a);
mysql_query($query);


:

$a = ($b = 2 + 1);
$a = isset($a) ? $b = $a : $b = false;
$a = ($b = func1($a)) + func2($a);
$a = mysql_query($query = ‘SELECT …’);


:

// $a == true func1() func2()
$a ? func1() : func2();

// func1() func2() :
func1() + func2();

// func1() func2() $a > 0
$a <= 0 or func1() + func2();


PHP- , . .

, . , , :

$a = true or func1();

func1() , “true or” true, , . and:

$a = false and func1();

, or and || &&. (or and ). | & . , .

:
Func1() or func2() or func3();

« , - true», and – false. if:

$res = mysql_connect(…);
if (!$res) die();


:

$res = mysql_connect(…) or die();

, . , . :

mysql_connect(…) and mysql_select_db (…) and mysql_query("SET NAMES utf8") or die(mysql_error());

– , , , - .

, , . ( ) «, ».

. , .. .

, for while. . , , :

$res = mysql_query(“SELECT …”);
If ($res) while ($v = mysql_fetch_assoc($res))
{
// - .
}

:

for($res = mysql_query(“SELECT …”); $res&&($v = mysql_fetch_assoc($res));)
{
// - .
}

, for for($i = 0; $i != $k; $i++) 3 , , , . ( ). , «- » , :

for(
$res = mysql_query(“SELECT …”);
$res && ($v = mysql_fetch_assoc($res));
// - ,
print_r($v)
);


, for , :

for(
$res = mysql_query(“SELECT …”), $i = 0;
$res && ($v = mysql_fetch_assoc($res));
// - ,
print_r($i++), print_r($v)
);

php .

, , . , , DIR1 DIR2 . . .

if (is_file(DIR1.$file)) $path = DIR1.$file;
elseif (is_file(DIR2.$file)) $path = DIR2.$file;
else $path = false;

:

is_file($path = DIR1.$file) || is_file($path = DIR2.$file) || $path = false;

, - is_file true, $path. , if-elseif-elseif… switch-case-case…

, , if. , , .


')

Source: https://habr.com/ru/post/38639/


All Articles