在PHP中,變量賦值有多種方式。以下是一些常見的賦值方法:
標量變量賦值:
$variable = 42;
$variable = 3.14;
$variable = 'Hello, World!';
$variable = true;
或 $variable = false;
數組變量賦值:
$variable = array('value1', 'value2', 'value3');
$variable = array('key1' => 'value1', 'key2' => 'value2');
$variable = array(array('value1', 'value2'), array('value3', 'value4'));
復合類型變量賦值:
$variable = array('key1' => 'value1', 'key2' => 'value2');
$variable = array(1, 2, 3, 4, 5);
$variable = new ClassName();
或 $variable = (object) array('property1' => 'value1', 'property2' => 'value2');
通過函數返回值賦值:
$variable = function_name();
$variable = function_name();
$variable = function_name();
使用可變變量(Variable Variables):
$variable = 'value';
${variable} = 'new_value';
使用可變函數(Variable Functions):
$function = 'strtolower';
$result = $function('Hello, World!');
這些是PHP中常見的變量賦值方式。