BinSpeak

dev@blog

Archive for the 'hint' Category

editing php on the go

Juli 21st, 2009 by admin

I found a great site, which offers an webbased php editor with a lot of common features like syntax highlighting or ftp access. On phpanywhere.net you can register for free to get an account for the beta realease of their editor. This is a great way to access your php files on the go, whithout the need of an locally installed environment. My first impression was good, although i haven’t tested it enough yet, to write a complete review. One of the biggest problems might be, that you have to giva away your ftp-login data to the creators of phpanywhere, but for small apps or presentation sites this is no real loss.

Try it, and tell me , what you think of it.

Category: hint | 167 Comments »

eval ist böse

Oktober 31st, 2008 by admin

Heute habe ich mal wieder einen eval-Ausdruck in dem PHP-Code an dem ich gerade arbeite gefunden, der in etwa folgendermaßen aussah (stark vereinfacht dargestellt):

<?
    function calc($a, $b, $sign) {
        eval ('$z = $a ' . $sign . ' $b;');
        return $z;
    }

    $x    = 5;
    $y    = 3;
    echo calc($x, $y, '+') . "\n";
    echo calc($x, $y, '-');

    // Output:
    // 8
    // 2
?>

Das Ziel der Funktion ist klar, man will abhängig von einem Vorzeichen entweder addieren oder subtrahieren. Nun ist eval ja böse, ist hier aber auch gar nicht nötig, weil man das ganze auch mit einem intlösen kann, der 1 oder -1 ist.

<?
    function calc($a, $b, $sign) {
        return $a + $sign * $b;
    }

    $x    = 5;
    $y    = 3;
    echo calc($x, $y, 1) . "\n";
    echo calc($x, $y, -1);

    // Output:
    // 8
    // 2
?>

Ab und zu hatte ich schon einen bool-Wert der festlegte, wie zu rechnen ist, auch den kann man direkt an die Funktion übergeben, das ganze sieht dann so aus:

<?
    function calc($a, $b, $sign) {
        return $a + ((2*$sign)-1) * $b;
    }

    $x    = 5;
    $y    = 3;
    echo calc($x, $y, true) . "\n";
    echo calc($x, $y, false);

    // Output:
    // 8
    // 2
?>

Hier macht man sich zunutze, das true bei Berechnungen zu 1 ausgewertet wird. 2*$sign-1 ergibt dann eben 1 wenn $sign=true (also 1) ist, und es ergibt -1 wenn $sign=false (also 0) ist. So man kann die Variable Sign in einem Bit (z.B. in einer Datenbank) speichern.

Category: hint | 72 Comments »

Eclipse Projekt in PHP Projekt umwandeln

Oktober 28th, 2008 by admin

Heute habe ich zum ersten mal ein neues Eclipse-Projekt mit Hilfe der Checkout-Funktion von Subclipse erstellt. Das hat auch alles wunderbar geklappt, allerdings hat Eclipse nicht automatisch ein PDT/PHP-Projekt erstellt, was dazu geführt hat, das er die einzelnen PHP-Files des Projekts nicht geparst, und somit auch keine Code-Vervollständigung angeboten hat. Da man den Typ des Projekts anscheinend nicht in Eclipse einstellen kann, musste ich direkt in der .project-Datei Änderungen vornehmen.Die beiden Blöcke BuildSpec und Natures müssen so angepasst werden, das folgendes Schema entsteht:

<projectDescription>
  <buildSpec>
    <buildCommand>
      <name>org.eclipse.php.core.PhpIncrementalProjectBuilder</name>
      <arguments>
      </arguments>
    </buildCommand>
    <buildCommand>
      <name>org.eclipse.wst.validation.validationbuilder</name>
      <arguments>
      </arguments>
    </buildCommand>
  </buildSpec>
  <natures>
    <nature>org.eclipse.php.core.PHPNature</nature>
  </natures>
</projectDescription>

Nach einem Neustart war das Projekt ein PHP Projekt (zu erkennen an dem kleinen P im Icon)
und Eclipse hat sofort alle Files geparst und mir alle Klassen und Funktionen angezeigt.

Category: hint | 405 Comments »