質問

I want to echo a value, but hold it as a variable to access that value later.

  1. What's the right syntax for the following?

    if (!empty($row['A'])){
        $Test = echo '<span>sale</span>';
    }
    
  2. How do i prevent the above PHP block from printing and only print when that variable when is called somewhere like as in.

    echo $test;
    
  3. Is there a way to put function inside echo?

like.

echo 'echo this is a 'if (!empty($row['A'])){
    $Test = echo '<span">function</span>';
}''.

I know the above is wrong, please give a reason for your downvote if you are gonna downvote. Thanks.

役に立ちましたか?

解決

You don't need to store echo in the variable. When you want to use the variable later, that is when you call echo. Your code should look like:

$test = (!empty($row['A'])) ? 'sale' : '';

This is a ternary operator which is basically a shorthand for the following if/else:

if(!empty($row['A'])) {
    $test = 'sale';
} else {
    $test = '';
}

In this case, I set it to an empty string if $row[a] is empty so nothing bad happens if you echo it later. (You want to make sure your variable is defined no matter what, so you don't cause an error by trying to call an undefined variable.)

Then, to use it, simply call

echo $test;

Why do you want to put the function inside of an echo? That defeats the point of storing a variable in the first place.

他のヒント

I think what you would want is something like this:

echo "This is a " . returnValue();

function returnValue() {
return "function";
}

The function is now set to return the value "function", in the echo we echo some text and the return value, so what it should echo is: "This is a function"

Assuming you're trying to check if the given variable is empty and then store some text in it, the correct syntax would be:

if (!empty($row['A'])){
    $test = '<span>sale</span>';
}

The above code reads: if $row['A'] is not empty, store $test equal to the string given.

Now, you can re-use the variable in your code.

I'm not quite sure what you're trying to accomplish with the second code block, but I assume you're trying to echo the variable value only if it's set. In that case, you can use the isset() function:

echo isset($test) ? $test : '';

This is the shorthand for the following:

if (isset($test)) {
    echo $test;
} else {
    echo '';
}

First of all, you should do some tutorials and read up on the basics. That said here is what I think you're looking for. For one thing you never do $var = echo 'blah';

First example.

if (!empty($row['A'])){
    $Test = '<span>sale</span>';
    echo $Test;
}

Then you can use $Test later in the code if you want. To avoid it echoing just remove the echo $test and do it elsewhere.

For your second part using a variable and a ternary is the best option for this

$isItEmpty = empty($row['A']) ? 'empty' : 'not empty';
echo 'This is row is '.$isItEmpty;

You can also to a ternary inline like this, but it's cleaner to use a variable usually.

echo 'This is row is '.(empty($row['A']) ? 'empty' : 'not empty');

This would output: "This row is empty" if it's empty and "this row is not empty" if it's not (both examples would have the same output.

Hope this helps

You don't need to include the echo in the variable.

Where you have

$Test = echo '<span>sale</span>';

Instead have

$Test = '<span>sale</span>';

Then when you want to echo it, simple

echo $Test;

If you would like to echo it according to the if then you can:

if (!empty($row['A'])){
    $Test = '<span>sale</span>';
echo $Test;    
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top