du4373 2013-09-13 21:40
浏览 165
已采纳

根据PHP中的另一个变量更改变量的值

first let me say I am trying very hard to learn PHP, and the more I learn, the more I want to learn and use the language. I'm primarily an HTML5/CSS3 coder, and am trying to make my sites more efficient by using PHP.

Here is my current dilemma. I am attempting to utilize variables to change values as required. At the beginning of each page file I have declared the page name, called my functions file and called head and header functions:

<?php
    $page = 'contact';
    include('functions.php');
    head();
    header();
?>

A slight variation of this is included in all page files. Things are good.

Now, in my functions.php I have a variable for the meta title value, i.e. $title, and have declared it as a global.

How do I change the value of this variable based on the value of the $page variable? Here is what I have added to the functions.php file, but I am quite sure it is incorrect, as it is not working:

$title = array {
    if ($page == 'contact') {
        echo 'Contact Us';
    }
}

I'm not just looking for the right code, but an explanation if possible. Thanks in advance for your assistance.

  • 写回答

4条回答 默认 最新

  • dsxd62219570 2013-09-13 21:44
    关注

    You can store all titles in an associative array:

    $TITLES = array(
        'contact' => 'Contact Us',
        'home' => 'Welcome',
    );
    
    $title = "Some Default Title"; 
    
    if(isset($TITLES[$page])){ //check to see if we have a specific title set up 
    
        $title = $TITLES[$page]; //override it if it is set 
    
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog