For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > January 2006 > beginning of a string









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author beginning of a string
Brian

2005-11-28, 7:55 am

Hi

I should know this but never had to do it, I'm trying
to work out what the beginning of a string contains
What I'm trying to do is this

$s = 'K123456'

if the beginning of a string is 'K' then do X if
beginning of a string is 'T' then do Y
and so on




Kimmo Laine

2005-11-28, 7:55 am

"Brian" <not@given.com> wrote in message
news:wODif.4411$uR.2961@newsfe7-gui.ntli.net...
> Hi
>
> I should know this but never had to do it, I'm trying
> to work out what the beginning of a string contains
> What I'm trying to do is this
>
> $s = 'K123456'
>
> if the beginning of a string is 'K' then do X if
> beginning of a string is 'T' then do Y
> and so on



switch($s{0}){
case 'K':
// do something
break;
case 'T':
// do something else
break;
default:
// error handler
}

--
"En ole paha ihminen, mutta omenat ovat elinkeinoni." -Perttu Sirviö
antaatulla.sikanautaa@gmail.com.NOSPAM.invalid


Marcel

2005-11-28, 7:55 am


"Brian" <not@given.com> schreef in bericht
news:wODif.4411$uR.2961@newsfe7-gui.ntli.net...
> Hi
>
> I should know this but never had to do it, I'm trying
> to work out what the beginning of a string contains
> What I'm trying to do is this
>
> $s = 'K123456'
>
> if the beginning of a string is 'K' then do X if
> beginning of a string is 'T' then do Y
> and so on
>



if($s{0} == 'K') {
// do something
}
if($s{0} == 'T') {
// do something else
}


Brian

2005-11-28, 6:57 pm


"Marcel" <sorryafraidofspam@nospam.com> wrote in message
news:d6b42$438b0bf2$d969db07$29182@cache
30.multikabel.net...
>
> "Brian" <not@given.com> schreef in bericht
> news:wODif.4411$uR.2961@newsfe7-gui.ntli.net...
>
>
> if($s{0} == 'K') {
> // do something
> }
> if($s{0} == 'T') {
> // do something else
> }
>

See i knew is was an easy answer and i should have known it
Thanks guys



anonymus

2006-01-10, 4:00 am

<?php

$substring = substr("K123456", 0, 1);

if($var == "K")
{
// code here if the string begins with "K"
}
elseif($var == "T")
{
// code here if the string begins with "T"
}
?>


David Haynes

2006-01-10, 4:00 am

anonymus wrote:
> <?php
>
> $substring = substr("K123456", 0, 1);
>
> if($var == "K")
> {
> // code here if the string begins with "K"
> }
> elseif($var == "T")
> {
> // code here if the string begins with "T"
> }
> ?>
>
>

or
<?php
$s = "K123456";

switch( $s[0] ) {
case 'K':
// code for K
break;
case 'T':
// code for T
break;
}
?>

Charlie King

2006-01-14, 3:55 am

On Sat, 07 Jan 2006 17:05:49 -0500, in
<%WWvf.23054$%h2.16196@fe18.usenetserver.com> (alt.comp.lang.php)
David Haynes <david.haynes2@sympatico.ca> wrote:


> or
> <?php
> $s = "K123456";
>
> switch( $s[0] ) {
> case 'K':
> // code for K
> break;
> case 'T':
> // code for T
> break;
> }
> ?>


or

<?php
$s = "K123456";

switch( $s[0] ) {
case 'K':
// code for K
break;
case 'T':
// code for T
break;

default:
// code for when neither is true

}
?>

:)
--
Charlie
Jim Michaels

2006-01-16, 3:55 am

I thought the syntax for PHP to access a string 's chars was $s{0} in the
manual.
does [0] really work in practice?
"Charlie King" <charlie@removethisitsaspamtrap.stopthatitssilly.com> wrote
in message news:2qdhs1l83afgqk9t5qnfhiffqt97oic9sd@
4ax.com...
> On Sat, 07 Jan 2006 17:05:49 -0500, in
> <%WWvf.23054$%h2.16196@fe18.usenetserver.com> (alt.comp.lang.php)
> David Haynes <david.haynes2@sympatico.ca> wrote:
>
>
>
> or
>
> <?php
> $s = "K123456";
>
> switch( $s[0] ) {
> case 'K':
> // code for K
> break;
> case 'T':
> // code for T
> break;
>
> default:
> // code for when neither is true
>
> }
> ?>
>
> :)
> --
> Charlie



Charlie King

2006-01-16, 3:55 am

On Mon, 16 Jan 2006 00:26:34 -0800, in
< BbudnXquaNggxVbenZ2dnUVZ_sidnZ2d@comcast
.com> (alt.comp.lang.php)
"Jim Michaels" <jmichae3@yahoo.com> wrote:

> "Charlie King" <charlie@removethisitsaspamtrap.stopthatitssilly.com> wrote
> in message news:2qdhs1l83afgqk9t5qnfhiffqt97oic9sd@
4ax.com...

<snip>
[color=darkred]
> I thought the syntax for PHP to access a string 's chars was $s{0} in the
> manual.
> does [0] really work in practice?


You are right but, on my server at least, both syntaxes work.
--
Charlie
David Haynes

2006-01-16, 3:55 am

Jim Michaels wrote:
> I thought the syntax for PHP to access a string 's chars was $s{0} in the
> manual.
> does [0] really work in practice?
> "Charlie King" <charlie@removethisitsaspamtrap.stopthatitssilly.com> wrote
> in message news:2qdhs1l83afgqk9t5qnfhiffqt97oic9sd@
4ax.com...
>
>


Yes. $s[0] (which I read a 'ess at zero' works just fine.

RE: the use of default
It is not always necessary or desired. It depends upon the situation.
For example, having a K or T prefix on a string could simply set some
switches (booleans) that will be used later in the code.

-david-

David Haynes

2006-01-16, 3:55 am

David Haynes wrote:
> Jim Michaels wrote:
>
> Yes. $s[0] (which I read a 'ess at zero' works just fine.


Man! bad typing syndrome in action.

This should read:
$s[0] (which I read as 'string ess at zero') works just fine.
$s(0) does too, so does substr($s, 0, 1);

-david-

Kimmo Laine

2006-01-17, 3:55 am

"Charlie King" <charlie@removethisitsaspamtrap.stopthatitssilly.com> wrote
in message news:ccoms11jn5jca6e54q2kmo3m7d7aq9mf1h@
4ax.com...
> On Mon, 16 Jan 2006 00:26:34 -0800, in
> < BbudnXquaNggxVbenZ2dnUVZ_sidnZ2d@comcast
.com> (alt.comp.lang.php)
> "Jim Michaels" <jmichae3@yahoo.com> wrote:
>
>
> <snip>
>
>
> You are right but, on my server at least, both syntaxes work.



They both do work, but the square bracket syntax is deprecated, and the
curly bracket syntax should be used... and squares not. It is supported now,
but what happens if a version of php is released where they don't work
anymore... :)

"Note: For backwards compatibility, you can still use array-brackets for the
same purpose. However, this syntax is deprecated as of PHP 4."
From: http://www.php.net/manual/en/language.types.string.php

--
"En ole paha ihminen, mutta omenat ovat elinkeinoni." -Perttu Sirviö
spam@outolempi.net | Gedoon-S @ IRCnet | rot13(xvzzb@bhgbyrzcv.arg)


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com