doSomething(); if ($otherCondition == true) { doSomethingElse($someVariable); } else { doSomethingElse($otherVariable); } } // and better than above if ($someCondition == true) { doSomething(); if ($otherCondition == true) { doSomethingElse($someVariable); doSomething($someVariable); } else { doSomethingElse($otherVariable); doSomething($someVariable); } } /* using some php keyword */ //using static keyword for singleton pattern //or check data was loaded, // function get_data($data_key) { static $dataset; if (!isset($dataset[$data_key])) { //query database or something like that to get data //set data to static variable } return $dataset[$data_key]; }
public function __construct() { // do something if ($this->content == self::XYZ) { doSomething(); } }
function testStrictTypedArgument() { doEverything(123); // ok doEverything(123.4); // ok doEverything("123"); // ok doEverything("123.4"); // ok doEverything("abc"); // ok doSomething(123); // ok //doSomething(123.4); // Type error: ...must be of the type integer, float given //doSomething("123"); // Type error: ...must be of the type integer, string given //doSomething("123.4"); // Type error: ...must be of the type integer, string given //doSomething("abc"); // Type error: ...must be of the type integer, string given doEveryLittleThing(123); // ok doEveryLittleThing(123.4); // ok //doEveryLittleThing("123"); // Type error: ...must be of the type float, string given //doEveryLittleThing("123.4"); // Type error: ...must be of the type float, string given //doEveryLittleThing("abc"); // Type error: ...must be of the type float, string given //doAnything(123); // Type error: ...must be of the type string, integer given //doAnything(123.4); // Type error: ...must be of the type string, float given doAnything("123"); // ok doAnything("123.4"); // ok doAnything("abc"); // ok }
function f() { doSomething(); // OK doSomethingElse(); // NOK // NOK }
function f() { doSomething(); // OK doSomethingElse(); // NOK {{Remove this empty statement.}} // NOK }
/** * Documented function */ function duplicate($p1, $p2, $p3) { $a = $p1 + $p2; $b = doSomething($p3); while ($p1 < $p2) { $p1++; } return valueOf($a) - $b; }
function KO1() { // return in else and then if ($a5) { return $a; } else { $b = doSomething(); return $b; } $a++; }
protected function defaultEvent($aEvent) { $oForm = new weeForm('myform', 'update'); if (!empty($aEvent['post'])) { $aData = $oForm->filter($aEvent['post']); try { $oForm->validate($aData); // Validation success: process the form doSomething($aData); } catch (FormValidationException $e) { $oForm->fill($aData); $oForm->fillErrors($e); } } $this->set('form', $oForm); }
function f() { doSomething(); }
<?php doSomething(&$p1, $p2); // OK
function checkIdPass(){ $id =$_POST["eggId"]; $pass = $_POST["eggPassword"]; $link = mysql_connect('mysql010.phy.lolipop.lan', 'ユーザ名','パスワード'); if (!$link) { die('接続失敗です。'.mysql_error()); } $db_selected = mysql_select_db('LAA0535115-dbname', $link); if (!$db_selected){ die('データベース選択失敗です。'.mysql_error()); } mysql_set_charset('utf8'); $result = mysql_query('SELECT * FROM db_user'); if (!$result) { die('クエリーが失敗しました。'.mysql_error()); } while ($row = mysql_fetch_assoc($result)) { if($id == $row['id'] && $pass == $row['pass']){ $name = $row['name']; print"<br>「・・・。(こんにちは、"; print $name; print"さん。)」"; doSomething(1); } } }
<?php // Functions passed by value function doSomething($parameter) { $parameter = "Hello"; echo $Parameter; } $var = "Hey"; doSomething($var); echo $var;
<?php /** * What would be the value of $a and $b at the end of the program ? */ function doSomething(&$arg) { $return = $arg; $arg += 1; return $return; } $a = 3; $b = doSomething($a);
<?php doSomething(php_sapi_name()); // NOK doSomething(PHP_SAPI); // OK doSomething($a->php_sapi_name()); // OK
<?php /** * Function calls arguments indentation */ doSomething($p1, $p2); doSomething($p1, $p2); doSomething($p1, $p2); // NOK doSomething($p1, something($p1, $p2, $p3, $p4)); doSomething($p1, array($p1, $p2), $p2); doSomething($p1, $p2); // OK doSomething($p1, $p2); doSomething(anotherThing($p1, $p2, $p2)); /** * Method declaration argument indentation */ function f($p1, $p2) { } function g($p1, $p2) { } function h($p1, $p2) { } function j($p1, $p2) { } function k($p1, $p2)
<?php class MyTidy extends tidy { } function doSomething(MyTidy $o) { var_dump($o); } $o = new MyTidy(); var_dump($o instanceof MyTidy); doSomething($o);
<?php $o = new weeStringValidator(array('max' => 42)); $o->setValue($sValue); if ($o->hasError()) { // $sValue has a length greater than 42. echo $o->getError(); } else { // $sValue is valid, its length is smaller than or equal to 42, doSomething($sValue); }
characters <?php $a = doSomething(); ?> <html> <?php echo $a;
<?php $oUploads = new weeUploads(); foreach ($oUploads->filter('myfiles') as $oFile) { doSomething($oFile); }
<?php // função é um tipo! e pode ser guardada // em uma variável $func = function (&$param) { // aumenta 10% o valor $param *= 1.1; }; $value = 55; $func($value); echo $value, "\n\n"; // usado como callback function doSomething($callback) { echo "faz algo...\n"; // ... echo "e ao fim, chama o callback\n"; $callback(); } doSomething(function () { echo "callback executado!\n"; });
protected function defaultEvent($aEvent) { doSomething(); }
} for ($j = 0; $j < 42; $j++) { // OK for ($k = 0; $j++ < 42; $k++) { // NOK } } for ($i = 0; $i < 42; $i++) { (int) $i; // OK } for ($i = 0; $i < 10; $i++) { for ($k = 0; $k < 20; $i++) { // NOK echo "Hello"; doSomething($i = 0); // NOK } } for ($a->i++; $a->i < 3; $a->i++) { $a->i = 1; // NOK } for ($a->i++; $a->i < 3; $a->i++) { $a->z = 1; // OK } $i = 0; for (; $i > 0; $i++) { $i = 1; // OK
function outerFunction() { doSomething(); }
public function testMethod1() { doSomething(); }
function f() { if (false) { // OK } if (false) { // OK } else { } if (false) { // OK if (false) { // NOK {{Merge this if statement with the enclosing one.}} } } if (false) { // OK if (false) { // OK } doSomething(); } if (false) { // OK $a = 1; if (a) { // OK } } if (false) { // OK if (false) { // OK } } else { } if (false) { // OK if (false) { // OK } else { } } if (false) { // OK } else { if (false) { // OK if (false) { // NOK } } } if (false) { // OK if (true) { // NOK } } if (true) { // OK doSomething(); } if (false) { // OK while (true) { if (true) { // OK } } } while (true) { if (true) { // OK } } if (false) { // OK } if (false) { // OK doSomething(); } if (true) { if (false) { // NOK doSomething(); } } if (false) { if (false) { // OK } elseif (false) { } } if (false) { if (false) { // OK } } elseif (true) { } if (true) { // OK halt(); } }
<?php $o = new weeEmailValidator(); $o->setValue($sEmail); if ($o->hasError()) { echo $o->getError(); } else { doSomething($sEmail); } // valid
*/ if ($a) { // NOK {{Put one space between the closing parenthesis and the opening curly brace.}} } else { if ($b) { // NOK {{Put only one space between the closing parenthesis and the opening curly brace.}} } } if ($c) { // OK } function f() { } /** * Spacing inside parenthesis */ doSomething($p1, $p2); // NOK {{Remove all space after the opening parenthesis.}} doSomething($p1, $p2); // NOK {{Remove all space before the closing parenthesis.}} doSomething($p1, $p2); // NOK {{Remove all space after the opening parenthesis and before the closing parenthesis.}} doSomething($p1, $p2); // OK doSomething($p1, $p2); doSomething($p1, $p2); doSomething($p1, $p2); $flags = array(preg_match()); // NOK "{$space1}{$space0}SUM({$op})";
function f () { // NOK } if (true) // NOK { } $a = function () { doSomething; // NOK }; function f() { // OK } $var-> {'user_' . $id}; // OK if (true) { doSomething(); } // OK $var->{'user_' . $id}; { // OK echo $a; } if (true) { ?> // OK html <?php } if (true) { ?> html <? } // OK
doSomething(); } else { break; } } foreach ($OK3 as $b) { // no else if ($a3) { break; } } foreach ($OK4 as $b) { // return in else and then if ($a4) { break; } else { $b = doSomething(); break; } // Nothing after ifthen } foreach ($KO1 as $b) { // return in else and then if ($a5) { break; } else { $b = doSomething(); break; } $a++; }
<?php $oForm->validate($aData); // Validation success: process the form doSomething($aData);