Page 1 of 1

shorten ifelse

Posted: Wed Aug 27, 2008 6:11 am
by mad_phpq
Hi,

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

Code: Select all

empty($_POST['number']) ? $n ='1' : $n = $_POST['number'];

Re: shorten ifelse

Posted: Wed Aug 27, 2008 6:39 am
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.

Re: shorten ifelse

Posted: Wed Aug 27, 2008 7:10 am
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;

Re: shorten ifelse

Posted: Wed Aug 27, 2008 7:24 am
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);

Re: shorten ifelse

Posted: Wed Aug 27, 2008 7:38 am
by mad_phpq
you win! :)

Re: shorten ifelse

Posted: Wed Aug 27, 2008 7:39 am
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.

Re: shorten ifelse

Posted: Wed Aug 27, 2008 8:59 am
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'];
}

Re: shorten ifelse

Posted: Wed Aug 27, 2008 9:02 am
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)

Re: shorten ifelse

Posted: Wed Aug 27, 2008 9:14 am
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? :)

Re: shorten ifelse

Posted: Wed Aug 27, 2008 9:16 am
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.

Re: shorten ifelse

Posted: Wed Aug 27, 2008 9:25 am
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.