Hawgang
Hawgang

Reputation: 31

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.

Upvotes: 3

Views: 12679

Answers (10)

riad
riad

Reputation: 7194

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

Upvotes: 2

user229044
user229044

Reputation: 239291

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

Upvotes: 1

Ruel
Ruel

Reputation: 15780

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

Upvotes: 2

Phill Pafford
Phill Pafford

Reputation: 85308

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

Upvotes: 1

KeatsKelleher
KeatsKelleher

Reputation: 10191

How about something like:

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

Upvotes: 1

Probably in this way,

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

Upvotes: 1

Alex Pliutau
Alex Pliutau

Reputation: 21957

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

Upvotes: 0

Sabeen Malik
Sabeen Malik

Reputation: 10880

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

Upvotes: 1

stevendesu
stevendesu

Reputation: 16791

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

Upvotes: 11

VoteyDisciple
VoteyDisciple

Reputation: 37803

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

Upvotes: 3

Related Questions