Solved

PHP Syntax

Posted on 2009-05-04
3
166 Views
Last Modified: 2013-12-12
Why isn't this working?  It always evaluates as incorrect, even if it's correct.
<?php
function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}
 
echo curPageURL();
echo ' (pulled url)<br>';
 
$currentURL = curPageURL();
 
echo $currentURL;
echo ' (variable url)';
 
if ( $currentURL == 'http://domain.net/members/$username/mystuff/stuff-home' ) {
$dynamicClass = 'selected';
echo '<br>correct';
} else {
$dynamicClass = 'NOTselected';
echo '<br>incorrect';
}
 
?>

Open in new window

0
Comment
Question by:bschwarting
  • 2
3 Comments
 
LVL 1

Author Comment

by:bschwarting
ID: 24298313
It looks like variable in the == is causing the issue.  When I hard code in the variable, it works fine.  Why isn't it validating properly with the variable $username in it?
0
 
LVL 14

Accepted Solution

by:
EMB01 earned 500 total points
ID: 24298340
I think it's because you need to use double-quotes, like so. Whenever you use single quotes, you can't use dynamic variables.

Ref. http://us.php.net/variables#39194
<?php
function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}
 
echo curPageURL();
echo ' (pulled url)<br>';
 
$currentURL = curPageURL();
 
echo $currentURL;
echo ' (variable url)';
 
if ( $currentURL == "http://domain.net/members/$username/mystuff/stuff-home" ) {
$dynamicClass = 'selected';
echo '<br>correct';
} else {
$dynamicClass = 'NOTselected';
echo '<br>incorrect';
}
 
?>

Open in new window

0
 
LVL 1

Author Comment

by:bschwarting
ID: 24298355
genius!  it's always the small things.  thanks!
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question