I wonder why I am getting the result as 4
<?php
function minusNum($aNum){
$result=$aNum-1;
return $result;
}
$theNum=4;
minusNum($theNum);
echo $theNum;
?>
Your minusNum function returns a new value, it doesn't manipulate the inputted $aNum. You should use that returned value. E.g.:
$result = minusNum($theNum);
echo $result;
You need to capture the result from your function.
<?php
function minusNum($aNum){
$result = $aNum - 1;
return $result;
}
$theNum = 4;
$theNum = minusNum($theNum);
echo $theNum;
?>
Here you assign the result of the function back to your original variable $theNum.
Although you pass the variable into the function, it is only altered inside the function. The value remains the same outside of the function.
<?php
function minusNum($aNum){
$result=$aNum-1;
return $result;
}
$theNum=4;
echo minusNum($theNum);
?>
Echo the function because it returns result
but when you echo $theNum you will get the $theNum value which is 4.
Moreover the function will grape the value of any value variable that passes through not the variable it self. so you contradicting youself. I hope it helps.
Related
Say I got some function that run some code and then return something, like this:
function something()
{
//some code
return $some[$whatever];
}
So, if I want to extract the data I generated in the function - the new value for $some, how should I do it? for example this won't do anything:
echo ($some);
Or what am I missing here, please
Since your Function returns a value, You may need to catch & store it inside a variable and then echo the variable if it is a String or do some casting to that effect. Here's an example:
<?php
function something(){
//some code
$whatever = 3;
$some = ["Peace", "Amongst", "All", "Humanity"];
return $some[$whatever];
}
$var = something();
var_dump($var); //<== DUMPS :: "Humanity"
echo $var; //<== ECHOES:: "Humanity"
Test it out here.
Cheers and Good Luck....
You are trying to return a specif key from your array, which wasn't declared. I declared an array for you, and I added the isset to check if the key is existing in the array to prevent any php warnings.
function something($findKey)
{
$some = array('key'=> 123);
if(!isset($some[$findKey])) {
return false;
}
//some code
return $some[$findKey];
}
echo something('key');
I am clearing the concept of call-by-reference.Can anyone please explain the code lines below shown as an example of call-by-reference?
<?php
function test(){
$result = 10;
return $result;
}
function reference_test(&$result){
return $result;
}
reference_test($result);
?>
You have two problems.
$result is never set and the test function is never called.
You do a pass by reference on the wrong function. A pass by reference is to change the variable inside and outside the function.
Here is the solution, changed the function a bit to show you the difference. You don't need to return the variable you changed by reference.
// Pass by ref: because you want the value of $result to change in your normal code.
// $nochange is pass by value so it will only change inside the function.
function test(&$result, $nochange){
$result = 10;
$nochange = 10;
}
// Just returns result
function reference_test($result){
return $result;
}
$result = 0; // Set value to 0
$nochange = 0;
test($result, $nochange); // $result will be 10 because you pass it by reference in this function
// $nochange wont have changed because you pass it by value.
echo reference_test($result); // echo's 10
echo reference_test($nochange); // echo's 0
Iwonder why the out put is 0(zero) for below code snippet? Can anyone please clarify why below code output is zero.
<?php
function a($number)
{
return (b($number) * $number);
}
function b(&$number)
{
++$number;
}
echo a(5); // output 0(zero) ?
?>
You never return any value from the function, and you're trying to echo the return value.
function b(&$number)
{
return ++$number;
}
Note that this is a silly example for a function that takes its parameter by reference, since you don't have a reference to the original value 5. Something like this would be more appropriate:
function b( &$number) {
++$number;
}
$num = 5;
b( $num);
echo $num; // Prints 6
The function name is b, but you are calling a...
Also, you are echoing a function, that doesn't return a value. This means you are echoing a non-initialize variable.
You must either return a value:
return ++$number;
or echo the variable directly:
$number = 5;
b($number);
echo $number;
i've been coding asp and was wondering if this is possible in php:
$data = getData($isEOF);
function getData($isEOF=false)
{
// fetching data
$isEOF = true;
return $data;
}
the function getData will return some data, but i'd like to know - is it possible to also set the $isEOF variable inside the function so that it can be accessed from outside the function?
thanks
It is possible, if your function expects it to be passed by reference :
function getData(& $isEOF=false) {
}
Note the & before the variable's name, in the parameters list of the function.
For more informations, here's the relevant section of the PHP manual : Making arguments be passed by reference
And, for a quick demonstration, consider the following portion of code :
$value = 'hello';
echo "Initial value : $value<br />";
test($value);
echo "New value : $value<br />";
function test(& $param) {
$param = 'plop';
}
Which will display the following result :
Initial value : hello
New value : plop
Using the global statement you can use variables in any scope.
$data = getData();
function getData()
{
global $isEOF;
// fetching data
$isEOF = true;
return $data;
}
See http://php.net/manual/en/language.variables.scope.php for more info.
Yes, you need to pass the variable by reference.
See the example here : http://www.phpbuilder.com/manual/functions.arguments.php
so I have 2 functions like this:
function boo(){
return "boo";
}
and
function foo(){
echo "foo";
}
the fist one will return a value, and the 2nd one will output something to the screen directly.
$var = boo();
foo();
How can I merge these 2 functions into one, and somehow detect if it's being called to output the result to the screen, or if it's called for getting the return value? Then choose to use return or echo...
function boo_or_foo ($output = false) {
if ($output) {
echo "fbo";
} else {
return "foo";
}
}
But whats the benefit against just using one function (boo()) and echo it yourself?
echo $boo();
Well, a function should only do one thing, so typically you would have two functions. But, if you would like to combine them you can just check if is set:
function boo($var=null){
if(isset($var)) echo $var
else return "boo";
}
well return true in the function that prints then yo just do
function foo(){
echo "foo";
return true;
}
if(foo()){
echo "foo did print something";
}else{
echo "nope foo is broken";
}
I wanted to achieve the same effect. In my case I have functions that produce HTML which I want echoed directly sometimes (when an Ajax call is being made), or returned (when a call is made from another script).
For example, a function that creates a list of HTML <option> elements - listOfOption($filter). When one of my pages is first created, the function is called and the result is echoed in place:
<?= listOfOption($var) ?>
But sometimes the same data needs to be retrieved in an Ajax call:
http://site.com/listOfOption.php?parameter=2
Instead of writing two different scripts or specifying the behaviour in a parameter, I keep listOfOption($filter) in its own file like this:
if (__FILE__ == $_SERVER['SCRIPT_FILENAME'])
{
echo listOfOption($_REQUEST['parameter']);
}
function listOfOption($filter)
{
return '<option value="1">Foo</option>';
}
This way if the call is from another script, it returns the data; otherwise it prints the data.
Note that if a parameter isn't passed to the function I wouldn't have to do this, I could live with echoing the data always and replacing the <?= listOfOption() ?> invocation with <? listOfOption() ?> to keep things clear.