frame

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In Register

PHP Functions

PHP Functions

Create a function

<!DOCTYPE html>
<html>
<body>


<?php
function writeMsg() {
  echo "Hello world!";
}


writeMsg();
?>


</body>
</html>

Function with one argument

<!DOCTYPE html>
<html>
<body>


<?php
function familyName($fname) {
  echo "$fname Refsnes.<br>";
}


familyName("Jani");
familyName("Hege");
familyName("Stale");
familyName("Kai Jim");
familyName("Borge");
?>


</body>
</html>

Function with two arguments

<!DOCTYPE html>
<html>
<body>


<?php
function familyName($fname, $year) {
  echo "$fname Refsnes. Born in $year <br>";
}


familyName("Hege","1975");
familyName("Stale","1978");
familyName("Kai Jim","1983");
?>


</body>
</html>

Function with default argument value

<?php declare(strict_types=1); // strict requirement ?>
<!DOCTYPE html>
<html>
<body>


<?php
function setHeight(int $minheight = 50) {
  echo "The height is : $minheight <br>";
}


setHeight(350);
setHeight();
setHeight(135);
setHeight(80);
?>


</body>
</html>

Function that returns a value

<?php declare(strict_types=1); // strict requirement ?>
<!DOCTYPE html>
<html>
<body>


<?php
function sum(int $x, int $y) {
  $z = $x + $y;
  return $z;
}


echo "5 + 10 = " . sum(5,10) . "<br>";
echo "7 + 13 = " . sum(7,13) . "<br>";
echo "2 + 4 = " . sum(2,4);
?>


</body>
</html>


Return type declarations

<?php declare(strict_types=1); // strict requirement
function addNumbers(float $a, float $b) : float {
  return $a + $b;
}
echo addNumbers(1.2, 5.2); 
?>

Passing arguments by reference

<!DOCTYPE html>
<html>
<body>


<?php
function add_five(&$value) {
  $value += 5;
}


$num = 2;
add_five($num);
echo $num;
?>


</body>
</html>
Tagged:
Sign In or Register to comment.

Developer

| make vanilla clean and light
@ 2016 Your Sitename, All rights reserved. Material Design is a design language developed by Google. Material Design makes more liberal use of grid-based layouts and responsive animations and transitions.
Powered by VanillaForums, Designed by ThemeSteam

Contact us

info@example.com
support@example.com
(800) 3032120

Get In Touch