function maleToFemaleRatio($event_id) { $cxn = $GLOBALS['cxn']; $qry = "SELECT (SELECT sex \n FROM user_list \n WHERE user_list.user_id=attendees.user_id)\n as sexes FROM attendees \n WHERE event_id='{$event_id}'"; $res = mysqli_query($cxn, $qry); $males = 0; $females = 0; while ($row = mysqli_fetch_assoc($res)) { $sex = $row['sexes']; if ($sex == "M") { $males++; } if ($sex == "F") { $females++; } } if ($males != 0 and $females != 0) { return double($males) / double($females); } else { if ($males == 0 and $females == 0) { return "no attendees"; } else { if ($males == 0) { return "all female"; } else { if ($females == 0) { return "all males"; } else { return 0; } } } } }
function ethiopicmult($plier, $plicand, $tutor) { if ($tutor) { echo "ethiopic multiplication of {$plier} and {$plicand}\n"; } $r = 0; while ($plier >= 1) { if (!iseven($plier)) { $r += $plicand; } if ($tutor) { echo "{$plier}, {$plicand} ", iseven($plier) ? "struck" : "kept", "\n"; } $plier = halve($plier); $plicand = double($plicand); } return $r; }
Немного более сложными примерами выражений являются функции. Например, рассмотрим следующую функцию: */ function foo() { return 5; } function double($i) { return $i * 2; } $b = $a = 5; /* присвоить значение пять переменным $a и $b */ $c = $a++; /* постфиксный инкремент, присвоить значение $a (5) переменной $c */ $e = $d = ++$b; /* префиксный инкремент, присвоить увеличенное значение $b (6) переменным $d и $e */ /* в этой точке и $d, и $e равны 6 */ $f = double($d++); /* присвоить удвоенное значение $d перед инкрементом (2*6 = 12) переменной $f */ $g = double(++$e); /* присвоить удвоенное значение $e после инкремента (2*7 = 14) переменной $g */ $h = $g += 10; /* сначала переменная $g увеличивается на 10, приобретая, в итоге, значение 24. Затем значение присвоения (24) присваивается переменной $h, которая в итоге также становится равной 24. */
<?php $titrePage = "fonctions"; include "header.php"; //echo get_include_path(); require_once "fonctions.php"; //require parce que bloquant si non trouvé echo double(5) . "<br/>"; echo triple(5) . "<br/>"; echo $multiplicateur; echo double(triple(46)) . "<br/>"; // date du jour en format US $dateJour = date('Y-m-d'); // conversion en date FR echo date_fr($dateJour) . "<br/>"; echo date_fr('2015-10-05') . "<br/>"; echo date_us('05/10/2015') . "<br/>"; $tableau = array(array('Language' => 'PHP', 'Age' => 20, 'Createur' => 'Rasmus Lerdorf'), array('Language' => 'JAVA', 'Age' => 33, 'Createur' => 'SUN'), array('Language' => 'C#', 'Age' => 14, 'Createur' => 'Microsoft'), array('Language' => 'COBOL', 'Age' => 56, 'Createur' => 'IBM'), array('Language' => 'BASIC', 'Age' => 51, 'Createur' => 'inconnu'), array('Language' => 'C', 'Age' => 45, 'Createur' => 'inconnu'), array('Language' => 'C++', 'Age' => 17, 'Createur' => 'toto')); echo afficheTableau2D($tableau); $tNombres = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); echo '<pre>'; // affichage des nombres pairs print_r(array_filter($tNombres, 'pair')); // détail de la fonction array_filter $tResultat = array(); foreach ($tNombres as $cle => $nombre) { if (pair($nombre)) { $tResultat[$cle] = $nombre; } } print_r($tResultat);
<?php include "Header.php"; include "Double.php"; echo double(19) . "<br/>"; echo half(60) . "<br/>"; echo datefr("2015-10-05") . "<br/>"; echo dateus("05/10/2015") . "<br/>"; $tnombres = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); print_r(array_filter($tnombres, "pair")); print_r(array_filter($tnombres, "impair")); include "Footer.php";
<h1>Fonctions</h1> <?php get_include_path(); include "header.php"; include "fonction.php"; echo double(5) . "</br>"; echo triple(3) . "</br>"; echo double(triple(3)) . "</br>"; echo puissance(45, 1) . "</br>"; // date du jour en format US $dateJour = date('Y-m-d'); // conversion en date FR echo date_fr($dateJour) . "</br>"; // reconversion au format US echo date_us(date_fr($dateJour)) . "</br>"; include "footer.php";
<?php function double() { global $number; static $count; $number *= 2; $count++; echo $count . "回試行<br/>"; } $number = 3; for ($i = 0; $i < 20; $i++) { double(); } //関数を20回呼び出す処理を追加 echo $number; //課題7
<?php function double($i) { return $i * 2; } $b = $a = 5; $c = $a++; $e = $d = ++$b; $f = double($d++); $h = $g += 10; echo "{$a}"; // echo "<br/>\$a";
<?php function double(int $value) { return 2 * $value; } $a = double("5"); var_dump($a);
<table border = '1'> <tr> <th>EDIT</th> <th>ID</th> <th>Title</th> <th>URL</th> <th>State</th> </tr> <?php foreach ($apples as $k => $apple) { ?> <tr class = '<?php double($k); ?> '> <td> <a href = "index.php?controller=Guava&action=update&id=<?php echo $apple['id']; ?> "> Edit </a> </td> <td> <?php echo $apple['id']; ?> </td>
<?php include "header.php"; //echo get_include_path(); include "fonctionj4bis.php"; echo double(5) . '<br>'; echo triple(10) . '<br>'; echo double(triple(46)) . '<br>'; $dateJour = date('Y-m-d'); echo date_fr($dateJour) . "<br>"; echo date_us('05/10/2015') . "<br>"; $tableau = array(array('language' => 'PHP', 'age' => 21, 'createur' => 'Rasmus Ledorf'), array('language' => 'JAVA', 'age' => 33, 'createur' => 'SUN'), array('language' => 'C#', 'age' => 14, 'createur' => 'Microsoft'), array('language' => 'COBOL', 'age' => 56, 'createur' => 'IBM'), array('language' => 'BASIC', 'age' => 51, 'createur' => 'inconnu'), array('language' => 'C', 'age' => 45, 'createur' => 'inconnu'), array('language' => 'C++', 'age' => 32, 'createur' => 'toto')); echo afficheTableau($tableau) . "<br>"; $tnombres = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); echo '<pre>'; //affichage des nombres pairs print_r(array_filter($tnombres, 'pair')); $tresultat = array(); foreach ($tnombres as $nombre) { if (pair($nombre)) { $tresultat[] = $nombre; } } // affichage des nombres impairs print_r(array_filter($tnombres, 'impair')); // tri des éléments dont le créateur est inconnu print_r(array_filter($tableau, 'inconnu')); // calculs sur le tableau print_r(array_map('cube', $tnombres)); // ajout d'une zone divers dans le tableau print_r(array_map('divers', $tableau));