在使用json和PHP时,是否可以在元标题中添加$currentProduct的标题?
是否可以通过执行以下操作,将$currentProduct的标题显示在元标题中:在使用json和PHP时,是否可以在元标题中添加$currentProduct的标题?,php,json,seo,Php,Json,Seo,是否可以通过执行以下操作,将$currentProduct的标题显示在元标题中: <?php $root = $_SERVER["DOCUMENT_ROOT"]; $pageTitle = "Company Name | Our Products - <?php echo $currentProduct->title ?>"; ?> $productTitle = $currentProduct->title; 不确定代码顺序和标
<?php
$root = $_SERVER["DOCUMENT_ROOT"];
$pageTitle = "Company Name | Our Products - <?php echo $currentProduct->title ?>";
?>
$productTitle = $currentProduct->title;
不确定代码顺序和标记是否正确,但:
假设您将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:
2:
3:
文件
$pageTitle .= $currentProduct->title;
$pageTitle .= $currentProduct['title'];
现在相同,但有一个数组对象:
1:
2:
3:
文件
$pageTitle .= $currentProduct->title;
$pageTitle .= $currentProduct['title'];
注:
为了让PHP解释器在字符串中考虑变量,必须使用双引号。
至于花括号:
«任何带有字符串的标量变量、数组元素或对象属性
可以通过此语法包含表示。只需写下
表达式的显示方式与它在字符串外部的显示方式相同,并且
然后用{和}把它包起来。由于{不能转义,因此此语法将
仅当$紧跟在{后面时才能识别。使用{\$to
获取一个文本{$.»
看看这个
// 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
$pageTitle .= $currentProduct['title'];