473,405 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

PHP's 'or' operator

Xx r3negade
I've seen before in scripts something like this:
Expand|Select|Wrap|Line Numbers
  1. doSomething() or doSomethingElse()
  2.  
So in other words, if doSomething() fails, doSomethingElse() will execute.
I've tried to do the same thing:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. function doSomething($x)
  3. {
  4.   if ( $x != 0 )
  5.   {
  6.     throw new Exception('error!');
  7.   }
  8. }
  9. function doSomethingElse()
  10. {
  11.   print 'something else';
  12. }
  13. doSomething(0) or doSomethingElse();
  14. ?>
  15.  
The problem is, it *always* executes doSomethingElse(), regardless of whether an exception was thrown or not. Any help?

And yes, I know about "try...catch"; I just like to have a one-liner alternative.
Apr 27 '08 #1
3 1401
If you make the first function return a bool then it will only call the second function after the 'or' if it returns false.

If show is passed a 1 - "number is 1" is echoed.
If show is passed <> 1 - "number not 1" is echoed.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.   function show($x){
  3.     if($x==1){
  4.       echo "number is 1";
  5.       return true;
  6.     }else{
  7.       return false;
  8.     }
  9.   }
  10.  
  11.   function show2(){
  12.     echo "number not 1";
  13.   }
  14.  
  15.   show(1) or show2();
  16. ?>
  17.  
Apr 27 '08 #2
TheServant
1,168 Expert 1GB
I've seen before in scripts something like this:
Expand|Select|Wrap|Line Numbers
  1. doSomething() or doSomethingElse()
  2.  
So in other words, if doSomething() fails, doSomethingElse() will execute.
I've tried to do the same thing:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. function doSomething($x)
  3. {
  4.   if ( $x != 0 )
  5.   {
  6.     throw new Exception('error!');
  7.   }
  8. }
  9. function doSomethingElse()
  10. {
  11.   print 'something else';
  12. }
  13. doSomething(0) or doSomethingElse();
  14. ?>
  15.  
The problem is, it *always* executes doSomethingElse(), regardless of whether an exception was thrown or not. Any help?

And yes, I know about "try...catch"; I just like to have a one-liner alternative.
What about just doing:
[PHP]if ($x != 0)
{
throw new Exception('error!');
} else {
print 'something else';
}
[/PHP]

When you wirte doSomething(0) or doSomethingElse(); it makes no sense to the computer. It doesn't know which to choose because you have not given it any conditions. You need to say IF (this) {dothis} ELSEIF (that) {dothat}. You use OR like so: if (x ==1 OR x==0) {dothis} That's the logic anyway.
Apr 27 '08 #3
Motoma
3,237 Expert 2GB
Hi Xx r3negade,
MarkSponge is right on the money: The return value of your function is what makes the different. PHP utilizes a programming concept called "Short Circuit Evaluation." The return values from your functions are used to determine if the program continues to call functions or quit.

My apologies, I feel that I am inarticulate tonight, so I will show some code!

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. function returnTrue()
  4. {
  5.   echo 'returnTrue called!<br />';
  6.   return true;
  7. }
  8.  
  9. function returnFalse()
  10. {
  11.   echo 'returnFalse called!<br />';
  12.   return false;
  13. }
  14.  
  15. echo 'returnTrue() or returnFalse();<hr />'
  16. returnTrue() or returnFalse();
  17. echo '<hr />';
  18.  
  19. echo 'returnFalse() or returnTrue();<hr />'
  20. returnFalse() or returnTrue();
  21. echo '<hr />';
  22.  
  23.  
  24. echo 'returnTrue() and returnFalse();<hr />'
  25. returnTrue() and returnFalse();
  26. echo '<hr />';
  27.  
  28. echo 'returnFalse() and returnTrue();<hr />'
  29. returnFalse() and returnTrue();
  30. echo '<hr />';
  31.  
  32. ?>
  33.  
I hope that helps you out,
Motoma

I've seen before in scripts something like this:
Expand|Select|Wrap|Line Numbers
  1. doSomething() or doSomethingElse()
  2.  
So in other words, if doSomething() fails, doSomethingElse() will execute.
I've tried to do the same thing:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. function doSomething($x)
  3. {
  4.   if ( $x != 0 )
  5.   {
  6.     throw new Exception('error!');
  7.   }
  8. }
  9. function doSomethingElse()
  10. {
  11.   print 'something else';
  12. }
  13. doSomething(0) or doSomethingElse();
  14. ?>
  15.  
The problem is, it *always* executes doSomethingElse(), regardless of whether an exception was thrown or not. Any help?

And yes, I know about "try...catch"; I just like to have a one-liner alternative.
Apr 28 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

60
by: English Teacher | last post by:
Which would be more useful to learn, PHP or COLDFUSION? I know Coldfusion is popular in the work force. Is PHP? Thanks!
5
by: Alexander Baranovsky | last post by:
Let me to introduce myself. I'm author of the paxScript scripting engine: www.paxscript.com. My recent invention is the LISPPA technology (List Processing based on the Polymorphic Arrays): ...
88
by: Tim Tyler | last post by:
PHP puts most of its functions into a big flat global namespace. That leads to short function names - but creates a namespace minefield for programmers. Lots of the functions are legacies from...
48
by: Daniel Crespo | last post by:
Hi! I would like to know how can I do the PHP ternary operator/statement (... ? ... : ...) in Python... I want to something like: a = {'Huge': (quantity>90) ? True : False} Any...
12
by: DigDug | last post by:
I get two different results in doing a Xor on a large signed double between linux servers. working system is kernel 2.4 . bad results server is kernel 2.6. PHP version does not matter. I...
8
by: Skeleton | last post by:
I am using Windows XP + PHP 5.1.2 at home for development and using Linux + PHP 4.4.2 at server side. In my code, I am doing some bitwise operations. While doing this, I have come accross to...
5
by: tony | last post by:
I'm using PHP 5 on Win-98 command line (ie no web server involved) I'm processing a large csv file and when I loop through it I can process around 275 records per second. However at around...
7
by: Michael Sharman | last post by:
Can anyone confirm if in PHP 0 = FALSE and anything else if TRUE? Or is it more 0 = FALSE and 1 = TRUE? One of the reasons I ask is that if I do; //$count is an incrementing value starting...
15
by: fyi | last post by:
My new blog on bits and pieces of PHP coding. http://bitspiecesphp.blogspot.com/ Have used all of them. Hope they're as useful to others: Use PHP to track email sender from your web site...
9
by: Elizabeth Barnwell | last post by:
This is a tool for learning the PHP programming language: http://www.yoyobrain.com/subjects/show/3120 You can look through the tabs to find information, and you can have the site quiz you to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.