1

I want to define new variable in a function .

function show()
{//for e.g
    $name = "john";
    $family = "cina";
    $old = "45";
}
show();
echo "your name is $name ";//in another  line
echo "your family is $family ";//in another line
echo "your old is $old ";//in another  line

Thanks

mrjohn
  • 63
  • 4
  • 3
    Please see: http://stackoverflow.com/q/16959576/3933332 – Rizier123 Mar 03 '16 at 10:36
  • Variables created inside a function, only exist in that scope. Please read the post that Rizier123 linked. You can't use them anywhere else but inside the function where you created the variables – DarkBee Mar 03 '16 at 10:44
  • @Rizier123 Thanks but it dosnt help me. – mrjohn Mar 03 '16 at 10:45
  • @mrjohn Have you read it? PHP has function scope for variables, so you have to return your variables from the function (probably want to do it with an array) and then assign the return value back to variables in a `list()`. – Rizier123 Mar 03 '16 at 10:46

7 Answers7

3

You could also consider using list(); you'll have to make a slight change to your function but it'll work.

function show(){
   $name = "john";
   $family = "cina";
   $old = "45";

   return array($name,$family,$old);
}

list($name,$family,$old) = show(); 

echo $name;
echo $family;
echo $old;

Be aware that the order matters, the first value in the return array will be assigned to the first variable in the list(); and the second array value to the second variable in the list, and so on.

Epodax
  • 1,828
  • 4
  • 27
  • 32
2

Write echo inside function as below

function show()
{
   $name = "john";
   $family = "cina";
   $old = "45";
   echo $name." ".$family." ".$old;
}
show();
Divyesh Savaliya
  • 2,692
  • 2
  • 18
  • 37
2
function show()
{
   $name = "john";
   $family = "cina";
   $old = "45";
   return $name." ".$family." ".$old;
}
echo show();

This

Bramastic
  • 399
  • 4
  • 16
  • This work but i need to use variable in different lines . i want to just define variable.thanks – mrjohn Mar 03 '16 at 10:42
2

Return it from your function like this

function show()
{//for e.g
$name = "john";
$family = "cina";
$old = "45";
return $name . " " . $family . " " . $old;
}
$result = show();
echo $result;
1

you can use also in the following way

function show()
{
  $name = "john";
  $family = "cina";
  $old = "45";
  return $name." ".$family." ".$old;
 }
 echo      $res = show(); // john cina 45
Deep Kakkar
  • 5,831
  • 4
  • 39
  • 75
1

This is how I should do it =)

<?php

function show()
{
   $name = "john";
   $family = "cina";
   $old = "45";
   echo $name." ".$family." ".$old;
}
show();

?>
XsiSecOfficial
  • 954
  • 8
  • 20
0

As a general rule, don't do this. It's not very good and looks terrible, but this is what you need.

<?php 
$name = null;
$family = null;
$old = null;
function show()
{
   global $name, $family, $old;
   $name = "john";
   $family = "cina";
   $old = "45";
}

show();
echo "your name is $name ".PHP_EOL;
echo "your family is $family ".PHP_EOL;
echo "your old is $old ".PHP_EOL;

I will recommend an alternative way of writing this (one of many). You can check other answers for different ways.

class Singleton {
      public static $name;
      public static $family;
      public static $old;
}

function show()
{
   Singleton::$name = "john";
   Singleton::$family = "cina";
   Singleton::$old = "45";
}

show();
echo "your name is ".Singleton::$name;//in another  line
echo "your family is ".Singleton::$family;//in another line
echo "your old is ".Singleton::$old;//in another  line
apokryfos
  • 38,771
  • 9
  • 70
  • 114