Pass by Value

Passing a parameter to a function.

 

<?php

    function myFunction($myArg) { //the argument $myArg can be called anything

        echo "Hello, $myArg" ; //but must be kept consistent within the function
    }

	$myVar = "World!" ;

    myFunction($myVar) ;
?>

Displays:

Hello, World!

 

 

A default value can be set by argument assignment in the function interface.

 

<?php

	function myFunction($myArg = "World!") { //default value assigned

		echo "Hello, $myArg" ;
	}

	myFunction() ;
?>

 

Multiple parameters are received in to the function in the order they are sent by the function call.

 

<?php

	function myFunction($argTwo, $argOne, $argThree) {

		echo "$argTwo $argOne $argThree" ;
	}

	$myVar = "The answer to Life, the Universe and Everything is " ;

	$myNum = 42 ;

	$myPunc = "!" ;

	myFunction($myVar, $myNum, $myPunc) ;
?>

In the above, $myVar is the first parameter being sent in the function call, corresponding to $argTwo as the first argument in the function interface, and so on for subsequent parameters.

Leave a Reply