Notice: Trying to get property of non-object in user/plugins/metaseo/metaseo.plugin.php line 512
Archive - Felix Geenen - Webentwicklung

Short php-Tips

Bei Smashing-Magazine gibts im Moment php-Bücher zu gewinnen, Teilnahmebedingung war der Post von kurzen php-Tipps. Mein Beitrag gibts als Kopie auch hier im Blog:

---

I got three short tips for you:

##### 1. The "++-Tipp" #####

// it is very useful to know the difference between ++$var and $var++ (also works with --)

$value1 = $value2 = 6;

echo 'value1: ' . $value1++ . ' - value2: ' . ++$value2;
echo '<br>';
echo 'value1: ' . $value1 . ' - value2: ' . $value2;

// while ++$var first increases the var, $var++ is used before increasing


##### 2. The "dirname-Tipp" #####

// if you use files that are included in another script, it is helpful to use the dirname(__FILE__)
// function in includes, because it avoids trouble with paths later

// file1.php located at ../php/scripts/file1.php
<?php
include('../includes/file2.inc.php'); // will work
include(dirname(__FILE__) . '/../includes/file2.inc.php'); // would be better
?>

// file2.php located at ../php/includes/file2.inc.php
<?php
//include('./file3.inc.php'); // will not work!
include(dirname(__FILE__) . '/file3.inc.php'); // will work
?>

// file3.php located at ../php/includes/file3.inc.php
<?php
echo 'everything okay..';
?>

// so if you use the dirname(__FILE__)-function that returns the absolute path of the current
// file, you are safe if the file is included somewhere later


##### 3. The "short-if-Tipp" #####

// if you have to do a little decision in your code, the short if can be useful

$rand = round(rand());

echo '$rand is: ' . (($rand) ? 'one' : 'zero');

// the syntax is "(condition) ? return if condition is true : return if condition is false"

Greets, Felix

edit:
Ich habe die css-Datei für das Syntax-Highlighting-Plugin modifiziert (Scrollbar und Dateigröße verringert).
Wer sie findet, darf sie nutzen ;)

 1

Über

Benutzer