Solved

# how can I check for the firs three letters of the word with php?

Posted on 2016-10-20
85 Views
how can I check for the firs three letters of the word with php?
if my \$customerid variable starts with the text "934" I'd like to say "true" the otherwise I'd like to say "false"
0
Question by:Braveheartli
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 51

Assisted Solution

Rgonzo1971 earned 125 total points
ID: 41851731
HI,

pls try something like this

``````if (substr(\$customerid ,0,3) == "934") echo "yes";
``````
Regards
0

LVL 22

Accepted Solution

Ferruccio Accalai earned 250 total points
ID: 41851733
If (substr(\$customerid, 0, 3) == '934')
{ \$something = true; }
else
{ \$something = false;}

Edit: posted late :)
0

LVL 57

Assisted Solution

Julian Hansen earned 125 total points
ID: 41851735
You probably want to look at substr_compare() that does a comparisson on a substring
``````if (substr_compare(\$customerid, "934",0, 3)) {
// it starts with 934
}
``````
1

LVL 57

Expert Comment

ID: 41851740
substr_compare runs almost in linear time - the length of the string and substring have little impact on running time
the other method increases the longer the strings get.

Consider this sample
``````<?php
\$test = "934373648487548378374238943248726348268234";
\$sub = "9343736484875483783742389432487263482682";
\$len = strlen(\$sub);
\$start = microtime(true);
for(\$i = 0; \$i < 100000;\$i++) {
if (substr(\$test, 0, \$len) == \$sub) {
\$result = true;
}
else {
\$result = false;
}
}
\$end = microtime(true);
\$one = \$end - \$start;
\$start = microtime(true);
for(\$i = 0; \$i < 100000;\$i++) {
\$result = substr_compare(\$test, \$sub, 0, \$len);
}
\$end = microtime(true);
\$two = \$end - \$start;
echo "One: {\$one}<br/>";
echo "Two: {\$two}<br/>";
``````
Results for the above
``````One: 0.15969896316528
Two: 0.019267082214355
``````
With
\$sub="934"
``````One: 0.029422044754028
Two: 0.018805027008057
``````
0

LVL 1

Author Closing Comment

ID: 41851789
thank you all
0

LVL 57

Expert Comment

ID: 41851804
@Braveheartli,

Is there any particular reason you did not choose the first post as the correct answer - it is the same as the accepted post but was there first - by acknowledgement of the second poster?
0

LVL 1

Author Comment

ID: 41851809
Dear Julian Hansen,
I tried yours answer first, but somehow i couldn't get the result...most  probably my fault
0

LVL 57

Expert Comment

ID: 41851840
@Braverhertli,

I was not referring to my answer I was referring to Rgonzo1971.

``````if (substr(\$customerid ,0,3) == "934") echo "yes";
``````

This was posted first and is the same as the accepted answer.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

Building a glossary into your website 9 55
Can't find an open element in HTML 5 50
How can I split a variable 19 42
sitemap.xml 3 23
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites? Â It wants you to read and retype something that looks like this. Insanity! Â It's not EE's fault - that's just the way reCaptcha works. Â But it iâ€¦
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: Tâ€¦
The viewer will learn how to count occurrences of each item in an array.
###### Suggested Courses
Course of the Month8 days, 17 hours left to enroll