2017-02-24 55 views
-3

是否有可能做這樣的事情來獲得$currentProduct稱號元標題顯示:

<?php 
    $root = $_SERVER["DOCUMENT_ROOT"]; 
    $pageTitle = "Company Name | Our Products - <?php echo $currentProduct->title ?>"; 
?> 

$productTitle = $currentProduct->title; 

回答

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:

String Operators文檔

$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:

String Operators文檔

$pageTitle .= $currentProduct['title']; 

注:

對於PHP解釋器中必須使用「雙引號的字符串內代價─變量通吃。 至於大括號:

«與串 表示任何標量變量,數組元素或對象屬性可以通過該語法包括在內。只需使用與字符串外部相同的方式編寫 表達式,然後 將其包裝在{和}中。由於{不能被轉義,這個語法將只在$緊跟在{之後纔會被識別出來, 。用{\ $到 得到一個文字{$。»

看看這個answer

相關問題