Solved

PHP: Change a positive condition to negative.

Posted on 2009-04-07
2
413 Views
Last Modified: 2012-05-06
How can I reverse this condition in my function so that I don't need an else?
<?php
 
chdir_safe('../something\file.txt');
 
function chdir_safe($dir)
{	
	
	if (substr($dir,0,1) == "/" || substr($dir,0,1) == "\\" || substr($dir,1,1) == ":" ){
		//url is absolute
	}
	else
	{
		echo "fix the url<br>";
	}
	
	echo $dir;
	exit;
}
?>

Open in new window

0
Comment
Question by:level9wizard
[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 Comments
 
LVL 17

Expert Comment

by:Chris Harte
ID: 24087579
Use the logical 'not' prefix which is an exclamation mark "!"
<?php
 
chdir_safe('../something\file.txt');
 
function chdir_safe($dir)
{       
        
        if (!substr($dir, 0, 1) == "/" 
            || !substr($dir, 0, 1) == "\\" 
            || !substr($dir, 1, 1) == ":" )
        {
            echo "fix the url<br>";
        }
        
        echo $dir;
        exit;
}
?>

Open in new window

0
 
LVL 14

Accepted Solution

by:
shobinsun earned 500 total points
ID: 24088136

Hello,

Use this:


<?php
 
chdir_safe('../something\file.txt');
 
function chdir_safe($dir)
{      
       
        if (!(substr($dir,0,1) == "/" || substr($dir,0,1) == "\\" || substr($dir,1,1) == ":" )){
                     
                echo "fix the url<br>";
        }
       
        echo $dir;
        exit;
}
?>
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

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