简体   繁体   English

php语法意外的语法错误

[英]Php Syntax Unexpected Sytax Error

I want to write like this in php. 我想在php中这样写。 How can i express samely into php? 我怎样才能同样地表达成php?

$test = '{"longUrl": "http://www.yahoo.com"}';

Thanks. 谢谢。

If you want to write actual PHP code to make a new object (assuming your example is JSON), there's no literal/shortcut syntax in PHP for that; 如果您想编写实际的PHP代码来创建一个新对象(假设您的示例是JSON),则PHP中没有文字/捷径语法。 you have to make a new stdClass object and set its variables manually: 您必须创建一个新的stdClass对象并手动设置其变量:

$test = new stdClass;
$test->longUrl = "http://www.yahoo.com";

If you are comfortable writing JSON inside a string, as you are doing in your example, simply feed that into json_decode() and you have yourself a stdClass object: 如果您喜欢在字符串中编写JSON(如您在示例中所做的那样),只需将其输入json_decode() ,您将拥有一个stdClass对象:

$test = json_decode('{"longUrl": "http://www.yahoo.com"}');
$test = array("longUrl" => "http://www.yahoo.com");

>echo $test['longUrl']
http://www.yahoo.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM