Solved

PHP Syntax

Posted on 2009-05-04
3
169 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

705 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