Php function in echo? Is it possible?

Hi I have a special problem ... I have three values ​​from the database.

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

Values ​​are stored in variables:

$value1
$value2
$value3

These values ​​change from the database every time you download from a web page. I also have these meanings inside the echo ""; The php function is already similar:

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

it will be in another echo function from the first example, so in my opinion 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 that will not work. Any help would be appreciated. How to solve this problem? thank.

+3
source share
10 answers

, , . "" "". ... !

, :

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

. echo , if...else .

- , (...) ? ... : ....

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

value1==0. = , 0 value1.

+2

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

, ?a >

EDIT: , , .

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

- :

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

- :

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

3 1 .

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

, ?

I think you want 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 />";
        }
       }
   ');
?>
+1
source

Probably in this way

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

It is very difficult to say what you are trying to achieve, but, based on some of your other comments, I begin to suspect that you want PHP echo:

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

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

update:

Yes it is! I work, but I need different colors for two texts :-) How to add there?

I assume that you output the text to the 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\";";
+1
source
echo ($value1 == 0)?'Yes':'No';
0
source

Source: https://habr.com/ru/post/1769347/


All Articles