简体   繁体   中英

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

Please use value1==0 . Basically = means you assign the value 0 into the variable 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.

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
   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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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