<?php // Runs in PHP7, not PHP5 // Coercive mode function sumOfInts(int ...$ints) { return array_sum($ints); } ?> <html> <body style="font-size: 14pt;"> <img src="static/logo150.png"/> Hello, <?php echo $_SERVER['REMOTE_ADDR']; ?> <p> It is now <?php echo date(DATE_RFC2822); ?> <p> Welcome to PHP<?php echo sumOfInts(1, '2', 4); ?> <a href="http://php.net/">PHP</a>, running on a <a href="http://rumpkernel.org">rump kernel</a>... <p> Try <a href="phpinfo.php">phpinfo()</a>. </body> </html>
<?php // Coercive mode function sumOfInts(int ...$ints) { return array_sum($ints); } var_dump(sumOfInts('21', '3*', 4.1)); // int(9)
<?php /** * This kind of function declaration allows to pass as many parameters as we want * In PHP this is known as scalar type functions, an aproximation to the partials functions **/ // Coercive mode function sumOfInts(int ...$ints) { return array_sum($ints); } var_dump(sumOfInts(2, '3', 4.1, 20));
<head> <meta charset="UTF-8"> <title>Scalar Type Declarations</title> <link rel="stylesheet" type="text/css" href="../style.css"> <script src="../jquery.min.js" type="text/javascript" charset="utf-8"></script> <script src="../action.js" type="text/javascript" charset="utf-8"></script> </head> <body> <h2>Coercive</h2> <pre> // Coercive mode function sumOfInts(int ...$ints) { return array_sum($ints); } var_dump(sumOfInts(2, '3', 4.1)); </pre> <input id="display" type="button" value="Show" onclick="show('coercive');"> <div class="code coercive"> <?php function sumOfInts(int ...$ints) { return array_sum($ints); } var_dump(sumOfInts(2, '3', 4.1)); ?> </div> </body> </html>
<?php declare (strict_types=1); function sumOfInts(int ...$ints) { return array_sum($ints); } sumOfInts(2, '3', 4.1);