PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Lately, for short and sweetness I've been using the ternary operator for only caring about one of the two conditions (either just true or just false), like:
Is there anything wrong with that? If that's fine to do, what should I put as the do-nothing code...in that example I just put "false", what would you do?
superdezign wrote:You should only use ternary conditions if you have an outcome for when the condition is true and one for when it is false.
I know thats its purpose, but it works if for one of the outcomes I just put "filler code" (that doesn't do anything). There isn't anything wrong with that is there?
I'm not opposed to if statements, but I like the shortness of ternary.
Comes down to personal taste. Ternary can be useful in places but many people find it harder to read (no IF to read and recognise). I tend to use them only where I need to output one thing or another based on a variable. Most other cases I use a normal if. An example of the use of the ternary below.