It is currently Thu May 08, 2025 5:12 pm

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post Post subject: Quick conditionals
 
Offline
Godlike Poster
Godlike Poster
Years of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membership
User avatar

Joined: Sun Oct 16, 2005 9:42 am
Posts: 8798
Karma: 17

Location: Imagine in your mind a posh country club
Steam Login Name: azcn2503
For those who aren't in the know, here's a handy way of doing an if and else. It's handy in JS when you want to change something according to a certain status, and also works in PHP:

Instead of this:

[code:1:4541484f83]<?php
$value=2;
if($value<2){$value=2;}
else{$value=$value;}
?>[/code:1:4541484f83]

Use this:

[code:1:4541484f83]<?php
$value=2;
$value=$value<2?2:$value;
?>[/code:1:4541484f83]

Or in JavaScript, with the aid of the magical grab function:

[b:4541484f83]Magical grab function:[/b:4541484f83]
[code:1:4541484f83]<script type="text/javascript"><!--
function grab(o){
return document.all?document.all[o]:document.getElementById?document.getElementById(o):"";
}
//--></script>[/code:1:4541484f83]

You can change the visibility of things like so:

[code:1:4541484f83]onclick="grab('section').style.display=grab('section').style.display=='block'?'none':'block';"[/code:1:4541484f83]

Works a treat.

Basically:
setThisThing=[i:4541484f83]condition[/i:4541484f83]?thisWhen[i:4541484f83]Condition[/i:4541484f83]Is[b:4541484f83]True[/b:4541484f83]:thisWhen[i:4541484f83]Condition[/i:4541484f83]Is[b:4541484f83]False[/b:4541484f83];
Example: value = [i:4541484f83]is value less than 2[/i:4541484f83] ? make it a 2, otherwise : keep it as value

You can stack them up, too:

[code:1:4541484f83]$value=$value<2?$value>5?5:2:$value;[/code:1:4541484f83]

Instead of:

[code:1:4541484f83]
if($value<2){$value=2;}
else if($value>5:$value=5;}
[/code:1:4541484f83]

_________________
Follow your heart and live the dream <3


Thu Nov 17, 2005 11:33 am 
 Profile E-mail  
 
 Post Post subject:
 
Offline
Top Gun (Admin)
Top Gun (Admin)
Years of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membershipYears of membership
User avatar

Joined: Mon Sep 12, 2005 6:48 am
Posts: 23428
Karma: 11

Location: This forum.
Steam Login Name: Skillers1990
Hmm, useful. I'm guessing this is a refinement of existing code?

_________________
I was getting bored of this sig, and I realised I actually prefer small sigs ><
Join us on IRC! | TheSkillers.co.uk is under construction (still)!
Games: Steam | XFire | GFWL


Thu Nov 17, 2005 4:01 pm 
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Karma functions powered by Karma MOD © 2007, 2009 m157y