简体   繁体   English

回声中的Php功能? 可能吗?

[英]Php function in echo? Is it possible?

Hi I have a special problem ... I have three values from database. 嗨我有一个特殊问题...我有三个数据库值。

Value1 is 1 or 0
Value2 is again 1 or 0
Value3 is remaining time like (24 hours left, 23, ...)

There values are saved in variables: 值保存在变量中:

$value1
$value2
$value3

These values change from DB on every load from webpage. 这些值在网页的每次加载时都会从DB更改。 I have these values also inside echo""; 我有这些值也在echo“”里面; The php function is already like: php函数已经像:

echo"text text text .................
"Value 1 is:" . $value1 . " and value 2 is:" . $value2 . ""
..................;

I need a function that says different things like 我需要一个能说出不同内容的函数

if (value1=0)
echo "Only text"
else
echo "Value 1 is:" . $value1 . " and value 2 is:" . $value2 . ""; 

this to be in another echo function from first example so in my way it looks like this: 这是第一个例子中的另一个echo函数,所以在我的方式它看起来像这样:

*Some function*
echo"if (value1=0)
echo "Only text"
else
echo "Value 1 is:" . $value1 . " and value 2 is:" . $value2 . """; // two echo closing 

But it does not work. 但它不起作用。 Any help will be appreciated.How to solve this? 任何帮助将不胜感激。如何解决这个问题? thank you. 谢谢。

Echo is saying to output whatever you type. Echo说要输出你输入的内容。 Saying to output "echo" actually means to display the word "echo". 说输出“echo”实际上意味着显示“echo”这个词。 There's no reason to output an output... you already output it! 输出输出没有理由......你已经输出了!

I think what you want is something more like: 我想你想要的更像是:

if($value1 == 0){
    echo "Only text";
} else {
    echo "Value 1 is:" . $value1 . " and value 2 is:" . $value2;
}

Your second piece of code looks fine. 你的第二段代码看起来很好。 Having multiple echo statements is not a problem, and using if...else to choose one is perfectly reasonable. 拥有多个echo语句不是问题,使用if...else来选择一个是完全合理的。

If you have to use just one for some reason, you can use the (...) ? ... : ... 如果由于某种原因你只需要使用一个,你可以使用(...) ? ... : ... (...) ? ... : ... ternary operator. (...) ? ... : ...三元运算符。

echo (value1 == 0) ? 'Only text' : "Value 1 is:" . $value1 . " and value 2 is:" . $value2 . """;

Please use value1==0 . 请使用value1==0 Basically = means you assign the value 0 into the variable value1 . 基本上=表示将值0赋值给变量value1

 
 
 
 
  
  
  function chVal($value1, $value2) { if ($value1 == 0) { echo "Only text"; } else { echo "Value 1 is:" . $value1 . " and Value 2 is:" . $value2; } }
 
 
  

Is that what you are looking for? 那是你在找什么?

EDIT : I think I get what you mean. 编辑 :我想我明白你的意思。

 
 
 
 
  
  
  function chVal ($val) { if ($val == 0) { return true; } else { return false; } }
 
 
  

if ($value1) {
    echo "Value 1 is:" . $value1 . "\n";
} else {
    echo "Only Text\n";
}

if ($value2) {
    echo "Value 2 is:" . $value2 . "\n";
} else {
    echo "Only Text\n";
}

How about something like: 怎么样的:

<?php
$value1=0;
$value2=1;
echo ($value1==0) ? "value 1 is :".$value1." and value 2 is ".$value2 : "only text";
?>

Why not use something like this: 为什么不使用这样的东西:

echo "Value 1 : ".func1($value1)." - Value 2 : ".func2($value2)." - Value 3 : ".func3($value3);

then you may have 3 functions or 1 depending on how complex is your logic. 然后你可能有3个函数或1个,具体取决于你的逻辑有多复杂。

function func1($value){
 if ($value == 0) return " zero ";
 else return " else ";
}

Is this what you're looking for? 这是你在找什么?

I think you're looking to display the PHP code itself: http://se.php.net/manual/en/function.highlight-string.php 我想你想要显示PHP代码本身: http//se.php.net/manual/en/function.highlight-string.php

<?php
   highlight_string('
      function chkValue($value1,$value2) {
        if($value1 == 0) {
           echo "Only Text<br />";
        } else {
           echo "Value 1 is: ".$value1." and Value 2 is: ".$value2."<br />";
        }
       }
   ');
?>

Probably in this way, 可能就是这样,

echo 'if (value1=0)
echo "Only text"
else
echo "Value 1 is:"' . $value1 . " and value 2 is:" . $value2 ;

It's very hard to tell what you're trying to achieve, but based on some of your other comments I'm beginning to suspect you want to echo PHP code: 很难说出你想要实现的目标,但基于你的其他一些评论,我开始怀疑你想要回应PHP代码:

$value = ($value == 0 ? 'Only Text' : "Value 1 is:$value1 and value2 is: $value2");

echo "echo \"$value\";";

update: 更新:

Yes thats it! 对,就是那样! Working but I need different colors for the two texts :-) How to add there? 工作但我需要两种文本的不同颜色:-)如何添加?

I'm assuming you're outputting the text to a browser, so something like this will work: 我假设你正在将文本输出到浏览器,所以这样的东西会起作用:

$value = '<span style="color:#'
  . ($value == 0 ? 'f00">Only Text' : "0f0\">Value 1 is: $value1 and value2 is:  $value2")
  . '</span>';

echo "echo \"$value\";";
echo ($value1 == 0)?'Yes':'No';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM