parboy Posted May 2, 2010 Share Posted May 2, 2010 I'm a newbie and can't get this If else statement to work. It's supposed to point to a file named "main_menu.js" in one of 2 folders (/js or/js2) depending on the current page - what's wrong?? <?php $bios = '<script type="text/javascript" src="/js2/main_menu.js"></script>'; $other = '<script type="text/javascript" src="/js/main_menu.js"></script>'; if($folder = 'bios') { echo "$bios"; } else { echo "$other"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/200413-is-my-syntax-wrong/ Share on other sites More sharing options...
foobarbaz Posted May 2, 2010 Share Posted May 2, 2010 You're doing a common newbie mistake. You need to use the comparison sign (==) to compare a variable: <?php $bios = '<script type="text/javascript" src="/js2/main_menu.js"></script>'; $other = '<script type="text/javascript" src="/js/main_menu.js"></script>'; if($folder == 'bios') { echo "$bios"; } else { echo "$other"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/200413-is-my-syntax-wrong/#findComment-1051706 Share on other sites More sharing options...
parboy Posted May 2, 2010 Author Share Posted May 2, 2010 That's what I thought too, but when I use the comparison sign (==) it outputs the 2nd value (../js2..) even when $folder == 'bios'. NOTE: I have the $folder value print at the bottom of the page to check the condition. Quote Link to comment https://forums.phpfreaks.com/topic/200413-is-my-syntax-wrong/#findComment-1051710 Share on other sites More sharing options...
foobarbaz Posted May 2, 2010 Share Posted May 2, 2010 That's what I thought too, but when I use the comparison sign (==) it outputs the 2nd value (../js2..) even when $folder == 'bios'. NOTE: I have the $folder value print at the bottom of the page to check the condition. This is precedural programming, $bios can't be accessed before it is assigned. You must set it into a session element or above the IF statement. Quote Link to comment https://forums.phpfreaks.com/topic/200413-is-my-syntax-wrong/#findComment-1051711 Share on other sites More sharing options...
PFMaBiSmAd Posted May 2, 2010 Share Posted May 2, 2010 Look at your code. js2 is not the second value. Quote Link to comment https://forums.phpfreaks.com/topic/200413-is-my-syntax-wrong/#findComment-1051713 Share on other sites More sharing options...
parboy Posted May 2, 2010 Author Share Posted May 2, 2010 I've moved the first part above the DOCTYPE, i.e., <?php $bios = '<script type="text/javascript" src="/js2/accordion.js"></script>'; $other = '<script type="text/javascript" src="/js/accordion.js"></script>'; ?> and left the conditional statement near the bottom of the head: <?php if($folder == 'bios') { echo "$bios"; } else { echo "$other"; } ?> Still points to ../js.. regardless of $folder value. So... how would I set it as a session element? TO PFMaBiSmAd: I wrote ../js2.. but meant ../js.. Sorry about the typo. Quote Link to comment https://forums.phpfreaks.com/topic/200413-is-my-syntax-wrong/#findComment-1051718 Share on other sites More sharing options...
PFMaBiSmAd Posted May 2, 2010 Share Posted May 2, 2010 $folder clearly does not contain exactly the string 'bios' What do you get when you echo it and what does var_dump() show (in case it contains some non-printing characters.) Quote Link to comment https://forums.phpfreaks.com/topic/200413-is-my-syntax-wrong/#findComment-1051720 Share on other sites More sharing options...
parboy Posted May 2, 2010 Author Share Posted May 2, 2010 It was procedural, I had the definition of $folder below the conditional statement. Problem solved, thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/200413-is-my-syntax-wrong/#findComment-1051722 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.