dongliang1996 2013-02-06 09:21
浏览 9
已采纳

有条件地分配一个值

I have this function:

function my_function($value1, $value2, $value3 = null){
    if($value1 || $value2 || $value3 == 'my_title' ){
        echo '<title>'.$value2.'</title>';
    }
}

my_function('my_css', 'my_title');

So as you can see I had to put in the echo $value2 becuase I cannot figure out how I would be able to use either $value 1,2 or 3. I was thinking this could be solved by doing something like this:

  if($value1 || $value2 || $value3 == 'my_title' $title = $value1 || $value2 || $value3 ){

Is this possible?

I hope you can understand what im trying to achieve here.

---------------------------------------------------- SOLVED --------------------------------------------------------

Thanks to rohitarora for the best answer for this question.

i would like to share with everyone what you have helped me make.

if you have this function

function template_content($value1, $value2, $value3 = null){ global $title;
$value = array($value1, $value2, $value3);
for($i=0;$i<count($value);$i++)
{
    if (strpos($value[$i],'title: ') === 0 ){
        $title = explode("title: ",$value[$i]);
        echo "\t", '<title>'.$title[1].'</title>', "
";
    } else if (strpos($value[$i],'css: ') === 0 ){
        $css = explode("css: ",$value[$i]);
        echo "\t", '<link rel="stylesheet" href="'.$css[1].'">', "
";
    } else if (strpos($value[$i],'js: ') === 0 ){
        $js = explode("js: ",$value[$i]);
        echo "\t", '<script src="'.$js[1].'"></script>', "
";
    }
}

}

you can call this function

<!DOCTYPE html>
<html>
<head>
<?php template_content('css: http://google.com/style.css', 'title: hello', 'js: http://code.jquery.com/jquery-latest.min.js'); ?>
</head>
<body>
</body>
</html> 

and you will get this amazing result

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="http://google.com/style.css">
    <title>hello</title>
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
</body>
</html> 
  • 写回答

4条回答 默认 最新

  • doufangzhang4454 2013-02-06 09:28
    关注
    function my_function($value1, $value2, $value3 = null) {
        // make a array of all your values
        $title = array($value1, $value2, $value3);
        // for loop will run as the size of array like here there are three element in array value1,2,3
        for ($i = 0; $i < count($title); $i++)
        {
            // check if the string is there in the ith element in array
            if ($title[$i] == 'my_title')
            {
                echo '<title>'.$title[$i].'</title>';
            }
        }
    }
    
    my_function('my_css', 'my_title');
    

    Do it in array, that's a better way.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题