SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot ameerulislam's Avatar
    Join Date
    Jul 2011
    Posts
    127
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    some times simple else if doesn't want to work >:<

    what is wrong with this?
    PHP Code:
    <div>
                            <?php if(in_array(1$_SESSION['jigowatt']['user_level'])) { ?>
                            
                             <h1><?php echo 'Hello'.$profile->getField('name')." ".$profile->getField('last_name')?></h1>
                            
                            <?php }?>
                            <?php else {?>
                            <h1><?php  echo 'Hello' ?></h1>
                            
                            <?php //else{} ?>
                        </div>
    getting this stupid erro
    PHP Parse error: syntax error, unexpected 'else' (T_ELSE)

  2. #2
    SitePoint Wizard
    Join Date
    Oct 2005
    Posts
    1,768
    Mentioned
    5 Post(s)
    Tagged
    1 Thread(s)
    You have a simple syntax error. You are forgetting to use the colon : at the end of your if and else statements which is necessary using PHP's alternate syntax and you are including opening and closing braces {} which cannot be used with this syntax. You also need an endif and any time you have a PHP statement you must terminate it with a semi-colon ;.

    Code:
    <?php if($name == 'cheesedude'):?>
      <h1>Hello <?php echo $name;?></h1>
    <?php else: ?>
      <h1>Hello there stranger!</h1>
    <?php endif; ?>
    PHP control structure alternate syntax
    Mixing PHP and HTML

  3. #3
    SitePoint Evangelist
    Join Date
    Oct 2005
    Location
    Michigan, USA
    Posts
    434
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You don't have to use the alternate if syntax. What you have will do except when you close the php tags after the if block, what happens is you're printing output before your "else".

    This part
    PHP Code:
                            <?php }?> 
                            <?php else {?>
    Needs to be
    PHP Code:
    <?php } else ?>
    - Robert

  4. #4
    SitePoint Zealot ameerulislam's Avatar
    Join Date
    Jul 2011
    Posts
    127
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Yeah thanks I finally figured that out. thanks.. Thanks @cheesedude as well .

    Quote Originally Posted by QMonkey View Post
    You don't have to use the alternate if syntax. What you have will do except when you close the php tags after the if block, what happens is you're printing output before your "else".

    This part
    PHP Code:
                            <?php }?> 
                            <?php else {?>
    Needs to be
    PHP Code:
    <?php } else ?>

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2011
    Posts
    66
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Change
    Code:
     <?php }?> 
                            <?php else {?>
    To

    Code:
     <?php } 
                            else {?>
    Ofcourse it wont work if you close PHP tag in the middle of the code..

  6. #6
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,810
    Mentioned
    141 Post(s)
    Tagged
    0 Thread(s)
    As an FYI, you may want to read this comment found on http://php.net/manual/en/function.in-array.php
    Be aware of oddities when dealing with 0 (zero) values in an array...

    This script:
    PHP Code:
    <?php
    $array 
    = array('testing',0,'name');
    var_dump($array);
    //this will return true
    var_dump(in_array('foo'$array));
    //this will return false
    var_dump(in_array('foo'$arrayTRUE));
    ?>
    It seems in non strict mode, the 0 value in the array is evaluating to boolean FALSE and in_array returns TRUE. Use strict mode to work around this peculiarity.
    This only seems to occur when there is an integer 0 in the array. A string '0' will return FALSE for the first test above (at least in 5.2.6).
    If you ever have the value 0 in your array, you could get unwanted results.... so be sure to test for that.
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •