shorten ifelse

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!

Moderator: General Moderators

Post Reply
mad_phpq
Forum Commoner
Posts: 85
Joined: Fri Apr 27, 2007 5:53 am

shorten ifelse

Post by mad_phpq »

Hi,

How can I, if at all, shorten this statement.

Code: Select all

empty($_POST['number']) ? $n ='1' : $n = $_POST['number'];
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: shorten ifelse

Post by onion2k »

Code: Select all

$n=$_POST['number'];$n=empty($n)?1:$n;
Why do you want to though? Readability is more important than brevity when it comes to code.

You should also be wary of using empty(), especially when you're handling numbers. If $_POST['number'] were to be '0' in this case $n would end up as 1. That's technically wrong.
mad_phpq
Forum Commoner
Posts: 85
Joined: Fri Apr 27, 2007 5:53 am

Re: shorten ifelse

Post by mad_phpq »

thanks. Yeah, someone gave me that here, i was sure there was a shorter way to do it. Here is what i was aiming for.

Code: Select all

$podcastNumber  =   $_POST['podcastNumber'];
$podcastNumber  =   !isset($podcastNumber) ? 1 : $podcastNumber;
marcth
Forum Contributor
Posts: 142
Joined: Mon Aug 25, 2008 8:16 am

Re: shorten ifelse

Post by marcth »

Do I win?

Code: Select all

require_once('Param.php');
 
Param::init('number', 1);
EDIT: Or, if you didn't want to set $_REQUEST['number'] do:

Code: Select all

require_once('Param.php');
 
$podcastNumber = Param::get('podcastNumber', 1);
Attachments
Param.zip
The Param Class
(1.04 KiB) Downloaded 42 times
mad_phpq
Forum Commoner
Posts: 85
Joined: Fri Apr 27, 2007 5:53 am

Re: shorten ifelse

Post by mad_phpq »

you win! :)
marcth
Forum Contributor
Posts: 142
Joined: Mon Aug 25, 2008 8:16 am

Re: shorten ifelse

Post by marcth »

Here's a link to the API Documentation for the Param Class
http://www.phpformcontrol.com/docs/api/ ... Param.html

I also attached a SimpleTest unit test for the class--You'll have to edit the includes at the top of your file to get it to work for your environment.
Attachments
test_param.zip
SimpleTest unit test for the Param Class
(999 Bytes) Downloaded 38 times
mad_phpq
Forum Commoner
Posts: 85
Joined: Fri Apr 27, 2007 5:53 am

Re: shorten ifelse

Post by mad_phpq »

without using your class, how can I shorten this? I know its along the same lines as above, but i havent got my head around it yet.

Code: Select all

if (isset($_REQUEST['blockId'])){
    $blockId = $_REQUEST['blockId'];
} else {
    $blockId = $_POST['blockId'];
}
Last edited by mad_phpq on Wed Aug 27, 2008 9:13 am, edited 1 time in total.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: shorten ifelse

Post by onion2k »

mad_phpq wrote:you win! :)
Err.. his code is 50 bytes (ignoring the include file). Mine was 38 bytes. I'm still winning. 8)
mad_phpq
Forum Commoner
Posts: 85
Joined: Fri Apr 27, 2007 5:53 am

Re: shorten ifelse

Post by mad_phpq »

onion2k wrote:
mad_phpq wrote:you win! :)
Err.. his code is 50 bytes (ignoring the include file). Mine was 38 bytes. I'm still winning. 8)
how did you work that out? :)
marcth
Forum Contributor
Posts: 142
Joined: Mon Aug 25, 2008 8:16 am

Re: shorten ifelse

Post by marcth »

Haha, Nice try! Here write this code out your way:

Code: Select all

 
Param::init('firstName', 'Default');
Param::init('lastName', 'Default');
Param::init('email', 'Default');
Param::init('telephone', 'Default');
Param::init('streetName', 'Default');
Param::init('streetNumber', 'Default');
Param::init('streetDirection', 'Default');
Param::init('unitType', 'Default');
Param::init('unitNumber', 'Default');
Param::init('city', 'Default');
Param::init('country', 'Default');
Param::init('comment', 'Default');
Param::init('company', 'Default');
Param::init('faxNumber', 'Default');
 
 
 
 
This took me about 30secs.

Edit: Also, if you run both are scripts 10,000 times, you'll see that your will be faster--by a few nano-seconds I imagine.
marcth
Forum Contributor
Posts: 142
Joined: Mon Aug 25, 2008 8:16 am

Re: shorten ifelse

Post by marcth »

LOL, and just to be argumentative

Code: Select all

 
<?php
require_once('Param.php');
 
$start = mktime();
 
for($i=0; $i<100000; $i++) {
  Param::init('firstName', 'Default');
  Param::init('lastName', 'Default');
  Param::init('email', 'Default');
  Param::init('telephone', 'Default');
  Param::init('streetName', 'Default');
  Param::init('streetNumber', 'Default');
  Param::init('streetDirection', 'Default');
  Param::init('unitType', 'Default');
  Param::init('unitNumber', 'Default');
  Param::init('city', 'Default');
  Param::init('country', 'Default');
  Param::init('comment', 'Default');
  Param::init('company', 'Default');
  Param::init('faxNumber', 'Default');
}
 
print 'time: ';
print mktime() - $start;
?>
 
That took 4 seconds to initialize the 14 fields 10,000 times.
Post Reply