Jump to content

Is my syntax wrong?


parboy

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/200413-is-my-syntax-wrong/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/200413-is-my-syntax-wrong/#findComment-1051706
Share on other sites

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 abo­ve the IF statement.

Link to comment
https://forums.phpfreaks.com/topic/200413-is-my-syntax-wrong/#findComment-1051711
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/200413-is-my-syntax-wrong/#findComment-1051718
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

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.