Pergunta

[[Fixed]] Was accessing the file directly in my browser, instead of through the localhost web server

--

I'm fairly new to PHP and trying to get the following to work.

This code is in the middle of a HTML page.

I've initialized and set the variable right above the if statement

I'm checking if the variable is '1', when I've set it to '3'.

My problem is, that it is still running the html code in the IF statement when it shouldn't be

Any idea's?

Thanks in advance

<?php
  $ad='3';
  if($ad=='1')
    {?>
    <p>Broken</p>
    <?php
  }
?>
Foi útil?

Solução

I found the problem.

You have created a HTML file and using php inside that which is totally wrong .

To get php code working you need to create a php file and write php code

create a php file by saving a file as filename.php and copy those contents there, which will work fine

Also make sure that you have php installed on you system.If not download and install wamp server

Outras dicas

You have to check the test field inside $_SESSION array! You are checking the whole array... :)

<?php
session_start();
$_SESSION['test']='2';

if($_SESSION['test']=='1')
{?>
   <!--HTML Code-->
<?php
}
?>

You have also to check if the $_SESSION var is set:

   session_start(); 

    if(isset($_SESSION['test']))
    {
        if($_SESSION['test']=='1')
        {
          //do stuff HTML code
        }
        elseif($_SESSION['test']=='2')
        {
          //do other stuff HTML code
        }

    }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top