Solved

PHP: Change a positive condition to negative.

Posted on 2009-04-07
2
412 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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

740 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