Solved

PHP: Change a positive condition to negative.

Posted on 2009-04-07
2
408 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 16

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now