是否可以通过执行以下操作来获取$currentProduct
的标题以显示在元标题中:
<?php
$root = $_SERVER["DOCUMENT_ROOT"];
$pageTitle = "Company Name | Our Products - <?php echo $currentProduct->title ?>";
?>
$productTitle = $currentProduct->title;
答案 0 :(得分:0)
不确定代码的顺序和标签是否正确但是:
假设您已将JSON字符串转换为JSON对象,是的,您可以。 在PHP中,您有几个选择: 要么继续将$ currentProject作为STDClass对象,要么将其转换为数组:
// STDClass Object (Access with 'STDObject->key' returns value)
$currentProduct = json_decode($json_string); // Converts to STDClass Object
// Array Object (Access with Array[key] returns value)
$currentProduct = json_decode($json_string, true); // Converts to Array Object
使用和STDClass对象:
1:
// Use '{' and '}' to specify the interpreter you're dealing with an object.
$pageTitle = "Company Name | Our Products - {$currentProduct->title}"; // assuming $currentProduct is a String at least
2:
// Using ' single quotes with this case is better
// but you can't put the variable inside it. (Not that you need it in this case)
$pageTitle = 'Company Name | Our Products - '.$currentProduct->title; // Concatenate the string
3:
$pageTitle .= $currentProduct->title;
现在相同但使用数组对象:
1:
// Use '{' and '}' to specify the interpreter you're dealing with an object.
$pageTitle = "Company Name | Our Products - {$currentProduct['title']}"; // assuming $currentProduct is a String at least
2:
// Using ' single quotes with this case is better
// but you can't put the variable inside it. (Not that you need it in this case)
$pageTitle = 'Company Name | Our Products - '.$currentProduct['title']; // Concatenate the string
3:
$pageTitle .= $currentProduct['title'];
注意:强>
对于PHP解释器 - 考虑 - 字符串中的变量,您必须使用“双引号”。 至于花括号:
«带字符串的任何标量变量,数组元素或对象属性 可以通过此语法包含表示。简单地写一下 表达式与字符串外部的表达方式相同,并且 然后将其包装在{和}中。由于{无法转义,因此将使用此语法 只有当$紧跟{时才会被识别。使用{\ $ to 获得文字{$。»
查看此answer。