PHP语法错误问题,使用参数正确显示标题

时间:2017-09-02 04:40:17

标签: php

我有一个PHP代码如下,但无法显示正确的PHP值标题 例如,在独立的基础上,它显示正确的数据

$this->CatName; - Displays category

$this->prodDet->prod_name; - Displays Product Name

$this->prodDet->v_location; - Displays Location

我想创建一个组合标题

Used <catname> <prod_name> for sale in <v_location>

Like
Used Fisher Milk Bottle for sale in Capetown

但是当我修改

中的代码时
  $title = 'Used' ($this->CatName $this->prodDet->prod_name) 'for Sale in' ($this->prodDet->v_location);

它显示语法错误但无法正常工作

这是完整的代码

<?php
defined('_JEXEC') or die;

jimport('joomla.application.component.view');




/**
* Prepares the document
*
* @return void
*
* @throws Exception
*/
protected function _prepareDocument()
{
    $app = JFactory::getApplication();
    $menus = $app->getMenu();
    $title = null;

    $menu = $menus->getActive();

    if ($menu) {
        $this->params->def('page_heading', $this->params->get('page_title', $menu->title));
    } else {
        $this->params->def('page_heading', JText::_('COM_USEDCAR_DEFAULT_PAGE_TITLE'));
    }

    $title = $this->params->get('page_title', '');

    if (empty($title)) {
        $title = $app->get('sitename');
    } elseif ($app->get('sitename_pagetitles', 0) == 1) {
        $title = JText::sprintf('JPAGETITLE', $app->get('sitename'), $title);
    } elseif ($app->get('sitename_pagetitles', 0) == 2) {
        $title = JText::sprintf('JPAGETITLE', $title, $app->get('sitename'));
    }

    $title = $this->CatName;
    $this->document->setTitle($title);
}
?>

任何人都可以帮助如何显示正确的标题

2 个答案:

答案 0 :(得分:5)

使用连接运算符.,阅读更多here

变化

$title = 'Used' ($this->CatName $this->prodDet->prod_name) 'for Sale in' ($this->prodDet->v_location);

$title = 'Used' . ($this->CatName .' '.$this->prodDet->prod_name). 'for Sale in' . ($this->prodDet->v_location);

答案 1 :(得分:1)

使用串联(.)运算符,如下所示: -

$title = 'Used '.($this->CatName.' '.$this->prodDet->prod_name).' for Sale in '.($this->prodDet->v_location);

注意: - 还要在变量和字符串之间添加空格,以便输出看起来不错。(我已经在我的解决方案中做过了)

参考: - String Operators