0

I'm trying to create a simple yes/no or true/false boolean type line to parse content depending on the answer.

Like this:

<?php
dangerous = "yes";
?>

<?php
dangerous = "no";
?>

A block down here similar to isset, that will parse if the answer above is yes, and to not appear if anything else other than yes is written.

<?php if dangerous = yes ?>
Dangerous content here.
<?php endif; ?>

I'm new to PHP, so I'm not sure what route to go with here.

Tendo
  • 3
  • 1
  • 1
    I hope that's just pseudo-code, because as is, it's invalid PHP and will never say anything OTHER than `Dangerous content here`, because you're doing assignments everywhere (`=`), not equality tests (`==`). – Marc B Feb 20 '14 at 23:05

7 Answers7

1
if($dangerous == "yes"){
    //do something
} else {
    //do something else
}
Paul Dessert
  • 6,363
  • 8
  • 47
  • 74
1

Firstly, this line here will always evaluate to true, because you are using a single = which is an assignment operator:

if($dangerous = 'yes') // this will always be true

You need to use == for a comparison operator, or === for strict comparison which takes variable types and values into consideration as well. For more info on the difference between the comparison operators, see here.

The way you're doing it currently is pretty widely used, but is not the best practice. Your variable $dangerous will be set every time, and you need to check for the value of it to determine whether to evaluate your conditions or not:

if($dangerous == 'yes') {
    // do dangerous stuff
}

Better practice, as you've said, is to use a boolean variable which will evaluate to true or false (above example in this same test will evaluate to true in both cases):

$dangerous = true; // or false;

if($dangerous) {
    // do dangerous stuff
}

Likewise, if it's not dangerous:

if(!$dangerous) {
    // pretty safe, but this will evaluate to true for false, null, zero etc
}

In this example, !$dangerous will evaluate to true when $dangerous is null, zero etc, so if you need a strict comparison for the value of false, you'll need the === comparison operator:

if($dangerous === false) {
    // false evaluates to true when comparing for a false value
    // false evaluates to false when comparing for a null value
}

Better to use a boolean variable over a string representation of a result in most cases. Something to keep in mind though is that if your your boolean variable represents the return of a function call, it might not always be consistent.

Community
  • 1
  • 1
scrowler
  • 24,273
  • 9
  • 60
  • 92
0
<?php
if($yes){
    ?>
        <b>This is my HTML.</b>
    <?php
}else{
    ?>
        <b>This is other HTML.</b>
    <?php
}
?>
Justin
  • 400
  • 1
  • 5
  • 16
0
<?php if ($dangerous === 'yes'): ?>
yes
<?php endif; ?>
<?php if ($dangerous == 'no'): ?>
no
<?php endif; ?>
Filip Górny
  • 1,749
  • 16
  • 24
0

The issue is the use of an assignment operator (=) when you should be using a comparison operator (== / ===) and comparing variables and value correctly.

For your case

<?php 
if($dangerous === "yes"){
  //Dangerous content here.
}
?>
thescientist
  • 2,906
  • 1
  • 20
  • 15
0

Use code like this:

$dangerous = true; // or false if no

// check result
if ($dangerous) {
    // dangerous!
}
LONGMAN
  • 980
  • 1
  • 14
  • 24
0

Define the conditions for dangerous content and apply php expeptions:

 if(dangerous){
 try {
   echo inverso(5) . "\n";
   echo inverso(0) . "\n";
 } catch (Exception $e) {
  echo 'Excepción capturada: ',  $e->getMessage(), "\n";
 }

 }