Solved

PHP Syntax

Posted on 2009-05-04
3
168 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Creating and Managing Databases with phpMyAdmin in cPanel.
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 and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

737 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