Solved

PHP: Change a positive condition to negative.

Posted on 2009-04-07
2
411 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
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
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 …

809 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