Short PHP Code


This is some example of short code in PHP
Exp1 :

// A lot of code
$status = fwrite($h, 'some text');
if (!$status) {
log('Writing failed');
}

// Less code
${0} = fwrite($h, 'some text');
if (!${0}) log('Writing failed');

// Even less code
fwrite($h, 'some text') or log('Writing failed');
Exp2 :

// A lot of code
if ($age < 16) {
$message = 'Welcome!';
} else {
$message = 'You are too old!';
}

// Less code
$message = 'You are too old!';
if ($age < 16) {
$message = 'Welcome!';
}

// Even less code
$message = ($age < 16) ? 'Welcome!' : 'You are too old!';
Exp3 :

// A lot of code
$i = 0;
while ($i < 100) {
$source[] = $target[$i];
$i += 2;
}

// less code
for ($i = 0; $i < 100; $source[] = $target[$i+=2]);