Tendo
Tendo

Reputation: 3

php for true/false parsing of content

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.

Upvotes: 0

Views: 78

Answers (7)

user3280814
user3280814

Reputation: 1

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

 }

Upvotes: 0

scrowler
scrowler

Reputation: 24425

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.

Upvotes: 1

LONGMAN
LONGMAN

Reputation: 990

Use code like this:

$dangerous = true; // or false if no

// check result
if ($dangerous) {
    // dangerous!
}

Upvotes: 0

thescientist
thescientist

Reputation: 2948

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.
}
?>

Upvotes: 0

Filip G&#243;rny
Filip G&#243;rny

Reputation: 1769

<?php if ($dangerous === 'yes'): ?>
yes
<?php endif; ?>
<?php if ($dangerous == 'no'): ?>
no
<?php endif; ?>

Upvotes: 0

Paul Dessert
Paul Dessert

Reputation: 6389

if($dangerous == "yes"){
    //do something
} else {
    //do something else
}

Upvotes: 1

Justin
Justin

Reputation: 410

<?php
if($yes){
    ?>
        <b>This is my HTML.</b>
    <?php
}else{
    ?>
        <b>This is other HTML.</b>
    <?php
}
?>

Upvotes: 0

Related Questions