开发者

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. I have these values also inside echo""; The php function is already like:

echo"text text text .................
"Value 1 is:"开发者_StackOverflow . $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:

*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. Saying to output "echo" actually means to display the word "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.

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 . """;


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";
}


Please use value1==0. Basically = means you assign the value 0 into the variable value1.


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.

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


Probably in this way,

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


How about something like:

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


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
   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 />";
        }
       }
   ');
?>


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:

$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';
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜