if not

Posted on 2014-12-01
Last Modified: 2014-12-01
Trying to do a simple test to see if a page has a / at the end of it:-
        if (!substr($_SERVER['REQUEST_URI'], -1) == "/") {
             die("unknown Request");

Open in new window

However every time it runs with or without a / it never runs the code. Even if I echo out the response I can see a / (or not, depending on how Im trying to debug) so its reading it right, but not running the if if it doesnt exist.

What am I missing here?
Question by:tonelm54
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
LVL 31

Accepted Solution

Marco Gasi earned 500 total points
ID: 40473573
Try this:

        if (substr($_SERVER['REQUEST_URI'], -1) != "/") {
             die("unknown Request");

Open in new window


Author Closing Comment

ID: 40473632
That works, but still dont understand why mine didnt work :-S

But thanks anyway
LVL 31

Expert Comment

by:Marco Gasi
ID: 40473680
Look here:
        if (! (substr($_SERVER['REQUEST_URI'], -1) == "/") ) {
             die("unknown Request");

Open in new window

The operator not (!) must be paced in front of the entire expression:

                                       if ( not ( is last char equal to slash? ) ) then...

This way it works, but it can be a bit confusing.
Thanks for points!

Expert Comment

by:Brian Tao
ID: 40473691
I know you've got an accepted solution, but I would just like to let you know what happened in your not-working code:
// in your code, PHP is evaluating !substr($_SERVER['REQUEST_URI'], -1) and compares it to "/", 
// so no matter what is in your REQUEST_URI, the left side evaluates to FALSE and never equals "/"
// this never "die" ... ^^
if (!substr($_SERVER['REQUEST_URI'], -1) == "/") {
     die("unknown Request");

// wrap the equation with a pair of parentheses and put the ! outside of it, and then it should work correctly
if (!(substr($_SERVER['REQUEST_URI'], -1) == "/")) {
     die("unknown Request");

Open in new window


Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

726 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