Jump to content
xisto Community
Sign in to follow this  
paulmason411

Php Variable Problems

Recommended Posts

Hi guys,

I'm new to php and I think i might be having problems with my syntax.

Heres a snippet of code that doesn't seem to work:

<?php $csslayout = 1; ?>					   <div id="forumposts">  <?php mosLoadModules ( 'user1' ); ?> /* can have the code <?php $csslayout = 2; ?> */</div><?php if (csslayout == 1){ ?>  <div id="content">	<?php mosMainBody(); ?>  </div><?php } ?><?php if (csslayout == 2){ ?>  <div id="content2">	<?php mosMainBody(); ?>  </div><?php } ?>

It never gets past the if statement.

What am I doing wrong?

Share this post


Link to post
Share on other sites

You were missing $ when calling your variable.

Try this:

<?php $csslayout = "1";					   echo "<div id='forumposts'>";mosLoadModules ("user1");echo "</div>";if ($csslayout == "1") { 	  echo "<div id='content'>";	  mosMainBody();	  echo "</div>";	}if ($csslayout == "2") { 	  echo "<div id='content2'>";	  mosMainBody();	  echo "</div>";	}?>

You can see that I've switched double quote with single quote when using PHP command that uses double quote. There is another way to use single quote command so that you can use double quote inside the PHP command. But try this first to see if it's working.

A neat trick I learned the first is the use of ECHO and PRINT command. By using this, you don't have to switch in and out from HTML and PHP. :P

Share this post


Link to post
Share on other sites

Well the code above should work. The reason yours was not working was that you were <?php and ?> after every php command. Your if statement was being opening and then you would stop using php and go to html. This would be seen in the server as an if statement without a body and would leave you with a brace not matched. If you are working with conditionals or loops in php make sure that you have the whole conditional or loop in the same php block or else you risk it not working correctly. To ensure you do this use the echo and print commands (like stated above) to send html to the page from the php block.

Share this post


Link to post
Share on other sites

The reason yours was not working was that you were <?php and ?> after every php command.

Not true, the reason he was doing this is to escape the PHP so he could put the HTML in. This did not cause the error.

Example:

<?php$var = "hi";if ($var == "hi"){ //if the variable $var is equal to "hi" carry on.echo 'Hey there!';}?>

Is the same as:

<?php$var = "hi";if ($var == "hi"){ //if the variable $var is equal to "hi" carry on.?>Hey there!<?php}?>

Also I think this is the other way to echo:

<?phpecho <<<EOFyou can put anything here really including " and 'EOF;?>

Hope this helps!

 

-Tom

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.