作爲一般規則,不這樣做。這不是很好,看起來很糟糕,但這是你需要的。
<?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;
我會推薦另一種寫這種方式(許多人之一)。您可以通過不同的方式查看其他答案。
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
請參閱:http://stackoverflow.com/q/16959576/3933332 – Rizier123
函數內部創建的變量只存在於該範圍內。請閱讀Rizier123鏈接的帖子。你不能在其他任何地方使用它們,而是在你創建變量的函數中使用 – DarkBee
@ Rizier123謝謝,但它dosnt幫助我。 – mrjohn