If the first four characters are "0000", then do {}

If the first four characters are "0000", then do {}

am 26.01.2010 03:36:50 von John Taylor-Johnston

I am reading the manual: http://ca.php.net/manual/en/ref.strings.php

$mydata->restored = "0000-00-00";

How do I express this? If the first four characters are "0000", then do {}

What I am looking for is in strpos(), no?

if (????????????)
{
}

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: If the first four characters are "0000", then do {}

am 26.01.2010 03:42:43 von Daniel Brown

On Mon, Jan 25, 2010 at 21:36, John Taylor-Johnston
wrote:
> I am reading the manual: http://ca.php.net/manual/en/ref.strings.php
>
> $mydata->restored = "0000-00-00";


$o[] = '0942-23-23';
$o[] = '0000-00-00';
$o[] = '1238-00-00';
$o[] = '0001-23-45';
$o[] = '0000-11-22';

for($i=0;$i if(preg_match('/^[0]{4,}\-/U',$o[$i])) {
echo "Offset #".$i." matches: ".$o[$i].PHP_EOL;
}
}
?>

--

daniel.brown@parasane.net || danbrown@php.net
http://www.parasane.net/ || http://www.pilotpig.net/
Looking for hosting or dedicated servers? Ask me how we can fit your budget!

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: If the first four characters are "0000", then do {}

am 26.01.2010 03:47:22 von Angus Mann

----- Original Message -----
From: "John Taylor-Johnston"
To: "PHP-General"
Sent: Tuesday, January 26, 2010 12:36 PM
Subject: [PHP] If the first four characters are "0000", then do {}


>I am reading the manual: http://ca.php.net/manual/en/ref.strings.php
>
> $mydata->restored = "0000-00-00";
>
> How do I express this? If the first four characters are "0000", then do {}
>
> What I am looking for is in strpos(), no?
>
> if (????????????)
> {
> }
>

if (substr($mydata,0,4) == "0000"{
Do stuff
}


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: If the first four characters are "0000", then do {}

am 26.01.2010 04:51:33 von Daevid Vincent

> -----Original Message-----
> From: parasane@gmail.com [mailto:parasane@gmail.com] On
> Behalf Of Daniel Brown
> Sent: Monday, January 25, 2010 6:43 PM
> To: John Taylor-Johnston
> Cc: PHP-General
> Subject: Re: [PHP] If the first four characters are "0000", then do {}
>
> On Mon, Jan 25, 2010 at 21:36, John Taylor-Johnston
> wrote:
> > I am reading the manual: http://ca.php.net/manual/en/ref.strings.php
> >
> > $mydata->restored = "0000-00-00";
>
> >
> $o[] = '0942-23-23';
> $o[] = '0000-00-00';
> $o[] = '1238-00-00';
> $o[] = '0001-23-45';
> $o[] = '0000-11-22';
>
> for($i=0;$i > if(preg_match('/^[0]{4,}\-/U',$o[$i])) {
> echo "Offset #".$i." matches: ".$o[$i].PHP_EOL;
> }
> }
> ?>

Holy macaroni. Talk about overkill!

if (substr($mydata->restored,0,4) == "0000") { }

Or in your very specific case you could do the harder way and note that
strings work like simple arrays too in a way, so $mydata->restored{0}
through $mydata->restored{3} should all be '0' ( note the {} and not [] ).
You can use a forloop or just manually check them all with && or || or
whatever logic you feel comfortable with. But the substr is by far the
simplest.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: If the first four characters are "0000", then do {}

am 26.01.2010 05:04:46 von Paul M Foster

On Mon, Jan 25, 2010 at 09:36:50PM -0500, John Taylor-Johnston wrote:

> I am reading the manual: http://ca.php.net/manual/en/ref.strings.php
>
> $mydata->restored = "0000-00-00";
>
> How do I express this? If the first four characters are "0000", then do {}
>
> What I am looking for is in strpos(), no?
>
> if (????????????)
> {
> }

From what I understand, strpos() faster than a lot of other similar
string functions and much faster than regexps. You could do:

if (strpos($mydata->restored, '0000') === 0) {
do_stuff();
}

Paul

--
Paul M. Foster

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: If the first four characters are "0000", then do {}

am 26.01.2010 05:12:43 von Daevid Vincent

> -----Original Message-----
> From: Paul M Foster [mailto:paulf@quillandmouse.com]
> Sent: Monday, January 25, 2010 8:05 PM
> To: php-general@lists.php.net
> Subject: Re: [PHP] If the first four characters are "0000", then do {}
>
> On Mon, Jan 25, 2010 at 09:36:50PM -0500, John Taylor-Johnston wrote:
>
> > I am reading the manual: http://ca.php.net/manual/en/ref.strings.php
> >
> > $mydata->restored = "0000-00-00";
> >
> > How do I express this? If the first four characters are
> "0000", then do {}
> >
> > What I am looking for is in strpos(), no?
> >
> > if (????????????)
> > {
> > }
>
> >From what I understand, strpos() faster than a lot of other similar
> string functions and much faster than regexps. You could do:
>
> if (strpos($mydata->restored, '0000') === 0) {
> do_stuff();
> }

Ah. Clever use of the "== 0" (tripple equals not necessary).
The OP needs to know if it's the first 4 or not and so position 0 would be
the start. Very nice.

Now, if his data is always of the format XXXX-XX-XX as it was illustrated,
then this will also work

if (strpos($mydata->restored, '0000-') !== false) { do stuff; }

(note the hyphen) and you should check the === false


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: If the first four characters are "0000", then do {}

am 26.01.2010 05:23:14 von Andrew Ballard

On Mon, Jan 25, 2010 at 11:12 PM, Daevid Vincent wrote:
>> >From what I understand, strpos() faster than a lot of other similar
>> string functions and much faster than regexps. You could do:
>>
>> if (strpos($mydata->restored, '0000') ===3D 0) {
>>       do_stuff();
>> }
>
> Ah. Clever use of the "== 0" (tripple equals not necessary).
> The OP needs to know if it's the first 4 or not and so position 0 would b=
e
> the start. Very nice.
>

The triple equals IS necessary. If '0000' is not found at all in
$mydata->restored, strpos() will return false, and (false == 0) in
PHP.

Andrew

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: If the first four characters are "0000", then do {}

am 26.01.2010 05:31:56 von Paul M Foster

On Mon, Jan 25, 2010 at 08:12:43PM -0800, Daevid Vincent wrote:

> > -----Original Message-----
> > From: Paul M Foster [mailto:paulf@quillandmouse.com]
> > Sent: Monday, January 25, 2010 8:05 PM
> > To: php-general@lists.php.net
> > Subject: Re: [PHP] If the first four characters are "0000", then do {}
> >
> > On Mon, Jan 25, 2010 at 09:36:50PM -0500, John Taylor-Johnston wrote:
> >
> > > I am reading the manual: http://ca.php.net/manual/en/ref.strings.php
> > >
> > > $mydata->restored = "0000-00-00";
> > >
> > > How do I express this? If the first four characters are
> > "0000", then do {}
> > >
> > > What I am looking for is in strpos(), no?
> > >
> > > if (????????????)
> > > {
> > > }
> >
> > >From what I understand, strpos() faster than a lot of other similar
> > string functions and much faster than regexps. You could do:
> >
> > if (strpos($mydata->restored, '0000') === 0) {
> > do_stuff();
> > }
>
> Ah. Clever use of the "== 0" (tripple equals not necessary).

No, the === was purposeful, since strpos can return false if there's no
match. If you simply use == 0, it will trigger on both false, and the
0 index. If you use !== false, you're only testing for no match.

Paul

--
Paul M. Foster

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: If the first four characters are "0000", then do {}

am 26.01.2010 11:31:28 von hSiplu

Another technique could be.

$s = $mydata->restored;

if($s[0]==$s[1]
&& $s[1]==$s[2]
&& $s[2]==$s[3]
&& $s[3]==0){

do_work();
}


But I think you got the best solution already.

--
Shiplu Mokaddim
My talks, http://talk.cmyweb.net
Follow me, http://twitter.com/shiplu
SUST Programmers, http://groups.google.com/group/p2psust
Innovation distinguishes bet ... ... (ask Steve Jobs the rest)

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: If the first four characters are "0000", then do {}

am 26.01.2010 14:45:49 von paragasu

maybe another way can be

$str = explode('-',$mydata->restored);

if($str[0] == '0000') { dostuff();}


;)

On 1/26/10, shiplu wrote:
> Another technique could be.
>
> $s = $mydata->restored;
>
> if($s[0]==$s[1]
> && $s[1]==$s[2]
> && $s[2]==$s[3]
> && $s[3]==0){
>
> do_work();
> }
>
>
> But I think you got the best solution already.
>
> --
> Shiplu Mokaddim
> My talks, http://talk.cmyweb.net
> Follow me, http://twitter.com/shiplu
> SUST Programmers, http://groups.google.com/group/p2psust
> Innovation distinguishes bet ... ... (ask Steve Jobs the rest)
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: If the first four characters are "0000", then do {}

am 26.01.2010 17:21:23 von Daniel Brown

On Mon, Jan 25, 2010 at 22:51, Daevid Vincent wrote:
>> -----Original Message-----
>> From: parasane@gmail.com [mailto:parasane@gmail.com] On
>> Behalf Of Daniel Brown
>> Sent: Monday, January 25, 2010 6:43 PM
>> To: John Taylor-Johnston
>> Cc: PHP-General
>> Subject: Re: [PHP] If the first four characters are "0000", then do {}
>>
>> On Mon, Jan 25, 2010 at 21:36, John Taylor-Johnston
>> wrote:
>> > I am reading the manual: http://ca.php.net/manual/en/ref.strings.php
>> >
>> > $mydata->restored =3D "0000-00-00";
>>
>> >>
>> $o[] =3D '0942-23-23';
>> $o[] =3D '0000-00-00';
>> $o[] =3D '1238-00-00';
>> $o[] =3D '0001-23-45';
>> $o[] =3D '0000-11-22';
>>
>> for($i=3D0;$i >> =A0 =A0 =A0 =A0 if(preg_match('/^[0]{4,}\-/U',$o[$i])) {
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 echo "Offset #".$i." matches: ".$o[$i].P=
HP_EOL;
>> =A0 =A0 =A0 =A0 }
>> }
>> ?>
>
> Holy macaroni. Talk about overkill!
>
> if (substr($mydata->restored,0,4) == "0000") { }

Overkill?


$o[] =3D '0942-23-23';
$o[] =3D '0000-00-00';
$o[] =3D '1238-00-00';
$o[] =3D '0001-23-45';
$o[] =3D '0000-11-22';

$now =3D microtime();

for($i=3D0;$i if(preg_match('/^[0]{4,}\-/U',$o[$i])) {
//echo "Offset #".$i." matches: ".$o[$i].PHP_EOL;
}
}

echo (microtime(1) - $now)."\n";


$later =3D microtime();

for($i=3D0;$i if(substr($o[$i],0,4) == "0000") {
//echo "Offset #".$i." matches: ".$o[$i].PHP_EOL;
}
}

echo (microtime(1) - $later)."\n";

?>

Sample Output:

1264522257.0001
1264522257

The preg_match() method, which is more expandable and adaptable
than relying on static position, took less than one-ten-thousandths of
a second longer to calculate than substr().

Just an FYI before you start worshipping pasta, Mr. Vincent. ;-P

--=20

daniel.brown@parasane.net || danbrown@php.net
http://www.parasane.net/ || http://www.pilotpig.net/
Looking for hosting or dedicated servers? Ask me how we can fit your budge=
t!

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: If the first four characters are "0000", then do {} - timing tests

am 26.01.2010 23:17:22 von Daevid Vincent

=20

> -----Original Message-----
> From: parasane@gmail.com [mailto:parasane@gmail.com] On=20
> Behalf Of Daniel Brown
> Sent: Tuesday, January 26, 2010 8:21 AM
> To: Daevid Vincent
> Cc: John Taylor-Johnston; PHP-General
> Subject: Re: [PHP] If the first four characters are "0000", then do {}
>=20
> On Mon, Jan 25, 2010 at 22:51, Daevid Vincent=20
> wrote:
> >> -----Original Message-----
> >> From: parasane@gmail.com [mailto:parasane@gmail.com] On
> >> Behalf Of Daniel Brown
> >> Sent: Monday, January 25, 2010 6:43 PM
> >> To: John Taylor-Johnston
> >> Cc: PHP-General
> >> Subject: Re: [PHP] If the first four characters are=20
> "0000", then do {}
> >>
> >> On Mon, Jan 25, 2010 at 21:36, John Taylor-Johnston
> >> wrote:
> >> > I am reading the manual:=20
> http://ca.php.net/manual/en/ref.strings.php
> >> >
> >> > $mydata->restored =3D "0000-00-00";
> >>
> >> > >>
> >> $o[] =3D '0942-23-23';
> >> $o[] =3D '0000-00-00';
> >> $o[] =3D '1238-00-00';
> >> $o[] =3D '0001-23-45';
> >> $o[] =3D '0000-11-22';
> >>
> >> for($i=3D0;$i > >> =A0 =A0 =A0 =A0 if(preg_match('/^[0]{4,}\-/U',$o[$i])) {
> >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 echo "Offset #".$i." matches: =
".$o[$i].PHP_EOL;
> >> =A0 =A0 =A0 =A0 }
> >> }
> >> ?>
> >
> > Holy macaroni. Talk about overkill!
> >
> > if (substr($mydata->restored,0,4) == "0000") { }
>=20
> Overkill?
>=20
> >=20
> $o[] =3D '0942-23-23';
> $o[] =3D '0000-00-00';
> $o[] =3D '1238-00-00';
> $o[] =3D '0001-23-45';
> $o[] =3D '0000-11-22';
>=20
> $now =3D microtime();
>=20
> for($i=3D0;$i > if(preg_match('/^[0]{4,}\-/U',$o[$i])) {
> //echo "Offset #".$i." matches: ".$o[$i].PHP_EOL;
> }
> }
>=20
> echo (microtime(1) - $now)."\n";
>=20
>=20
> $later =3D microtime();
>=20
> for($i=3D0;$i > if(substr($o[$i],0,4) == "0000") {
> //echo "Offset #".$i." matches: ".$o[$i].PHP_EOL;
> }
> }
>=20
> echo (microtime(1) - $later)."\n";
>=20
> ?>
>=20
> Sample Output:
>=20
> 1264522257.0001
> 1264522257
>=20
> The preg_match() method, which is more expandable and adaptable
> than relying on static position, took less than one-ten-thousandths of
> a second longer to calculate than substr().

Well allow me to retort... :)

Your test was for 5 measly array elements.

Just for S&G I tried it with the strpos one too and modified your test
slightly to handle bigger arrays...

for ($i =3D 0; $i < 1000000; $i++ )
$o[] =3D
sprintf('%04d-%02d-%02d',rand(0000,9999),rand(00,99),rand(00 ,99));
#print_r($o);
echo "array of ".number_format($i)."\n";
############################################################ #######
$now =3D microtime(true);
for($i=3D0;$i if(preg_match('/^[0]{4,}\-/U',$o[$i])) {
//echo "Offset #".$i." matches: ".$o[$i].PHP_EOL;
}
}
$rank['preg_match'] =3D (microtime(true) - $now);
############################################################ #######
$later =3D microtime(true);
for($i=3D0;$i if(substr($o[$i],0,4) == "0000") {
//echo "Offset #".$i." matches: ".$o[$i].PHP_EOL;
}
}
$rank['substr'] =3D (microtime(true) - $later);
############################################################ #######
$after =3D microtime(true);
for($i=3D0;$i if(strpos($o[$i], '0000') ===3D 0) {
//echo "Offset #".$i." matches: ".$o[$i].PHP_EOL;
}
}
$rank['strpos'] =3D (microtime(true) - $after);
############################################################ #######
asort($rank);
print_r($rank);
?>

array of 10,000
Array
(
[strpos] =3D> 0.00766682624817
[substr] =3D> 0.0116670131683
[preg_match] =3D> 0.0124950408936
)

array of 100,000
Array
(
[strpos] =3D> 0.0817799568176
[substr] =3D> 0.120522975922
[preg_match] =3D> 0.125612974167
)

array of 1,000,000
Array
(
[strpos] =3D> 0.805890083313
[substr] =3D> 1.19799995422
[preg_match] =3D> 1.25615906715
)

I ran out of memory with more than 1M array elements.

But yes, I will concede that the speed difference is minimal, even at 1M
elements. Although the docs are right that strpos is about 2x as fast...

> Just an FYI before you start worshipping pasta, Mr. Vincent. ;-P

By the way, I loved your book "Da Vinci Code" ;-p


ÐÆ5ÏÐ
http://daevid.com

Ezekiel 25:17. "The path of the righteous man is beset on all sides by =
the
inequities of the selfish and the tyranny of evil men. Blessed is he =
who,
in the name of charity and good will, shepherds the weak through the =
valley
of the darkness. For he is truly his brother's keeper and the finder of
lost children. And I will strike down upon thee with great vengeance and
furious anger those who attempt to poison and destroy my brothers. And =
you
will know I am the Lord when I lay my vengeance upon you!"


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: If the first four characters are "0000", then do {} -

am 26.01.2010 23:22:53 von parasane

On Tue, Jan 26, 2010 at 17:17, Daevid Vincent wrote:
>
> Well allow me to retort... :)
>
> Your test was for 5 measly array elements.
>
> Just for S&G I tried it with the strpos one too and modified your test
> slightly to handle bigger arrays...
>
[snip=3Dcode]
>
> I ran out of memory with more than 1M array elements.
>
> But yes, I will concede that the speed difference is minimal, even at 1M
> elements. Although the docs are right that strpos is about 2x as fast...

Right. I removed the note you posted to that effect a few moments
ago because you didn't explain what was being done. If you'll
resubmit it with a snippet of the conversation (or even just, "in
discussions about benchmarks with this vs. that vs. those, blah, blah,
blah....") it'll stay up. It's a good test result, and worthwhile for
those building large-scale systems. Kudos to you, Doctor! ;-P

>> =A0 =A0 Just an FYI before you start worshipping pasta, Mr. Vincent. =A0=
;-P
>
> By the way, I loved your book "Da Vinci Code" ;-p

I was always too busy writing it to read it.

--=20

daniel.brown@parasane.net || danbrown@php.net
http://www.parasane.net/ || http://www.pilotpig.net/
Looking for hosting or dedicated servers? Ask me how we can fit your budge=
t!

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: If the first four characters are "0000", then do {} -

am 27.01.2010 01:25:21 von hSiplu

Ran this code (http://pastie.org/795983)
The result is,

array of 10,000
Array
(
[[]] => 5.66168689728
[strpos] => 5.70796895027
[substr] => 5.92751288414
[preg_match] => 6.21515512466
)





--
Shiplu Mokaddim
My talks, http://talk.cmyweb.net
Follow me, http://twitter.com/shiplu
SUST Programmers, http://groups.google.com/group/p2psust
Innovation distinguishes bet ... ... (ask Steve Jobs the rest)

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: If the first four characters are "0000", then do {} - timing tests

am 27.01.2010 01:48:50 von Daevid Vincent

> -----Original Message-----
> From: muquaddim@gmail.com [mailto:muquaddim@gmail.com] On
> Behalf Of shiplu
> Sent: Tuesday, January 26, 2010 4:25 PM
> To: Daniel Brown
> Cc: Daevid Vincent; PHP-General
> Subject: Re: [PHP] If the first four characters are "0000",
> then do {} - timing tests
>
> Ran this code (http://pastie.org/795983)

+1 for using pastie.org. :)

> The result is,
>
> array of 10,000
> Array
> (
> [[]] => 5.66168689728
> [strpos] => 5.70796895027
> [substr] => 5.92751288414
> [preg_match] => 6.21515512466
> )

Almost 6 seconds to sort 10k array elements! Are you running this on a
Commodore64 ?!

Not that it probably matters, but I thought that it should be $o[$i]{0}
through $o[$i]{3}
Note the use of braces not square brackets for string character elements,
no?

Another thing I just noticed, is that we (that is Dan and I) should NOT
have used count()
This is bad form and wasted cycles.

for($i=0;$i
We should have just done a $elements = count($o) at the VERY top and used
$elements instead.

Does anyone know how PHP works internally? If I have 1M elements in the
array, does count() literally just zip through and $i++ them, or is there
some shortcut tally kept in the heap/stack/struct/memory somewhere for each
array?

Updated version:

for ($elements = 0; $elements < 1000000; $elements++ )
$o[] =
sprintf('%04d-%02d-%02d',rand(0000,9999),rand(00,99),rand(00 ,99));
#print_r($o);
echo "array of ".number_format($elements)."\n";
############################################################ #######
$time = microtime(true);
for($i=0;$i<$elements;$i++) {
if(preg_match('/^[0]{4,}\-/U',$o[$i])) {
//echo "Offset #".$i." matches: ".$o[$i].PHP_EOL;
}
}
$rank['preg_match'] = (microtime(true) - $time);
############################################################ #######
$time = microtime(true);
for($i=0;$i<$elements;$i++) {
if(substr($o[$i],0,4) == "0000") {
//echo "Offset #".$i." matches: ".$o[$i].PHP_EOL;
}
}
$rank['substr'] = (microtime(true) - $time);
############################################################ #######
$time = microtime(true);
for($i=0;$i<$elements;$i++) {
if(strpos($o[$i], '0000') === 0) {
//echo "Offset #".$i." matches: ".$o[$i].PHP_EOL;
}
}
$rank['strpos'] = (microtime(true) - $time);
############################################################ #######
$time = microtime(true);
for($i=0;$i<$elements;$i++) {
if($o[$i][0]==='0'
&& $o[$i][1]==='0'
&& $o[$i][2]==='0'
&& $o[$i][3]==='0'
) {
//echo "Offset #".$i." matches: ".$o[$i].PHP_EOL;
}
}
$rank['[]'] = (microtime(true) - $time);
############################################################ #######
asort($rank);
print_r($rank);
?>

array of 10,000
Array
(
[[]] => 0.00380492210388
[strpos] => 0.0054030418396
[substr] => 0.00824618339539
[preg_match] => 0.0103938579559
)

array of 1,000,000 (ran three times)
(
[[]] => 0.4429500103
[strpos] => 0.549595832825
[substr] => 0.847616195679
[preg_match] => 0.94566321373
)
(
[[]] => 0.420958995819
[strpos] => 0.55828499794
[substr] => 0.86266708374
[preg_match] => 0.933307886124
)
(
[[]] => 0.420862197876
[strpos] => 0.572381019592
[substr] => 0.855034828186
[preg_match] => 0.932211875916
)

Notice that without the 'count()' it is significantly faster than before...

array of 1,000,000
Array
(
[strpos] => 0.805890083313
[substr] => 1.19799995422
[preg_match] => 1.25615906715
)


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: If the first four characters are "0000", then do {}

am 27.01.2010 14:03:25 von M.Ford

> -----Original Message-----
> From: Daevid Vincent [mailto:daevid@daevid.com]
> Sent: 26 January 2010 03:52
>=20
> if (substr($mydata->restored,0,4) == "0000") { }
>=20
> Or in your very specific case you could do the harder way and note
> that
> strings work like simple arrays too in a way, so $mydata-
> >restored{0}
> through $mydata->restored{3} should all be '0' ( note the {} and not
> [] ).

Sorry, this is out of date and wrong. [] is the currently recommended way t=
o do string indexing, and {} is deprecated. See the Note at http://php.net/=
manual/en/language.types.string.php#language.types.string.su bstr.


Cheers!

Mike
--=20
Mike Ford,
Electronic Information Developer, Libraries and Learning Innovation,
Leeds Metropolitan University, C507, Civic Quarter Campus,=20
Woodhouse Lane, LEEDS,=A0 LS1 3HE,=A0 United Kingdom=20
Email: m.ford@leedsmet.ac.uk=20
Tel: +44 113 812 4730





To view the terms under which this email is distributed, please go to http:=
//disclaimer.leedsmet.ac.uk/email.htm

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: If the first four characters are "0000", then do {} -

am 27.01.2010 15:41:00 von Daniel Brown

On Tue, Jan 26, 2010 at 19:48, Daevid Vincent wrote:
>
> Another thing I just noticed, is that we (that is Dan and I) should NOT
> have used count()
> This is bad form and wasted cycles.

This is certainly correct, but it should also be noted that my
original code used it once (thus, it was in fine form) and it was only
to demonstrate a small field of results. Still, though a different
discussion, it's worth reminding folks to audit their own code to see
how many places they have unnecessary calls to count() when cycling
through the same array in multiple places.

Someone should start a "Pointers For Newbies, Reminders For
Oldies" thread on the list and let everyone participate with what
should be obvious - but are often overlooked - points within coding
practice that can cause the programmer to develop bad habits and bad
code. Who here would like to volunteer for the task? ;-P

--

daniel.brown@parasane.net || danbrown@php.net
http://www.parasane.net/ || http://www.pilotpig.net/
Looking for hosting or dedicated servers? Ask me how we can fit your budget!

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php