0

I tried this code today! But it's not giving the output I expected.. this is my code..

<?php

namePrint('Rajitha');

function namePrint($name) { 
  echo $name;
}

wrap('tobaco');

function wrap($txt) {
  global $name;
  echo "Your username is ".$name." ".$txt."";
}

?>

This code will print on screen

RajithaYour username is tobaco

but I want to get

RajithaRajithaYour username is tobaco

My question is: why is the $name variable in the wrap function not working?

Thanks.

4
  • Whats the point to namePrint? Why not just echo? Commented Aug 2, 2013 at 6:15
  • this is a sample structure of my complex code I want to get work $name variable in wrap() function Commented Aug 2, 2013 at 6:16
  • 1
    Shouldn't the expected output be RajithaYour username is Rajitha tobaco? Commented Aug 2, 2013 at 6:24
  • sorry , Yeh! it is :) Commented Aug 2, 2013 at 6:26

4 Answers 4

2

Never use echo inside function to output the result. And never use global for variables.

You used echo inside function and because of that you get unexpected output.

echo namePrint('Rajitha');

function namePrint($name){ 
    return $name;
}

echo wrap('tobaco');

function wrap($txt){
    //global $name;
    return "Your username is ".namePrint('Rajitha')." ".$txt."";
}

Output

using echo in function Codepad

RajithaRajithaYour username is  tobaco

Output1

using return in function Codepad

RajithaYour username is Rajitha tobaco
Sign up to request clarification or add additional context in comments.

3 Comments

OMG this is the answer, what Im looking for.. Thankyou verymuch sir!
oh! didnt downvote your answer.. I marked your answer as the correct one. thanks
@NadlerBanit I am not saying to you, it is for that person who downvoted. :)
1

If you want to wrap a function around another you could simply pass a closure as one of the arguments:

function wrap($fn, $txt)
{
    echo "Your username is ";
    $fn();
    echo ' ' . $txt;
}

wrap(function() {
    namePrint('Rajitha');
}, 'tobaco');

This construct is very delicate; using function return values is more reliable:

function getFormattedName($name) { 
    return $name;
}

echo getFormattedName('Jack');

Then, the wrap function:

function wrap($fn, $txt)
{
    return sprintf("Your username is %s %s", $fn(), $txt);
}

echo wrap(function() {
    return getFormattedName('Jack');
}, 'tobaco');

Comments

0

Another option would be to pass $name as a parameter to the wrap function.

<?php

$name = 'Rajitha';

function namePrint($name){ 
    echo $name;
}

function wrap($txt, $name){
    echo "Your username is " . $name . " ". $txt;
}

namePrint($name);

wrap('tobaco', $name);

?>

Comments

-1

$name should be declared and initialized as global variable.then you can the output you need.

The code should look like this.

<?php
$name = 'Rajitha';
namePrint($name);

function namePrint($name){ 
    echo $name;
}

wrap('tobaco');

function wrap($txt){
     global $name;
     echo "Your username is ".$name." ".$txt."";
}

?>

1 Comment

Everything that works does not necessarily mean good. This answer is misguiding for future readers because it uses global.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.