473,320 Members | 2,122 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

strings and conditionals..

hi, am new to PHP, trying to learn strings... I'm a bit mystifed about
something.. how do you test for conditions in strings? for example,
how would you do what in JavaScript (or Java) you do like this

if (email.indexOf('@') != -1) { // etc..

also how do you test for length of a string? I tried:

if (strlen($myName) == 7) {

but got error...

I have looked at sections dealing with conditionals in tutorials, but
don't see examles of this (also don't see functions like indexOf(),
etc.. under Strings here, http://us2.php.net/manual/en/ref.strings.php..

thank you very much..

Jun 11 '06 #1
8 1111
maya wrote:
hi, am new to PHP, trying to learn strings... I'm a bit mystifed
about something.. how do you test for conditions in strings? for
example, how would you do what in JavaScript (or Java) you do like
this
if (email.indexOf('@') != -1) { // etc..

also how do you test for length of a string? I tried:

You are looking for the position of a specific characther, so use the search
facility on php.net by entering "position" and selecting "all php.net sites"
or "this mirror only" from the select next to the input box. The first
search result is the one you are looking for.
if (strlen($myName) == 7) {

but got error...


When the error is a notice, the probable cause would be that you didn't
initialize $myName.

If you want to learn PHP (or any other new language for that matter), the
best way is to start from scratch; point your browser to
http://www.php.net/manual/en/getting-started.php to start with the basics.
JW
Jun 11 '06 #2
maya wrote:
if (email.indexOf('@') != -1) { // etc..
if (strstr(email, '@')!==FALSE)
{
// etc...
}
also how do you test for length of a string? I tried:
if (strlen($myName) == 7) {
but got error...


strlen() is correct. Your error is probably elsewhere.

See:
http://uk2.php.net/manual/en/ref.strings.php

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

Jun 11 '06 #3
Toby Inkster wrote:
maya wrote:

if (email.indexOf('@') != -1) { // etc..

if (strstr(email, '@')!==FALSE)
{
// etc...
}


thank you.. this is exactly what I was looking for ..
(noticed that it works both with '!==' and '!='.... there's probably
some subtle difference I'm still not aware of....:) thank you very much..

Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
Jun 12 '06 #4
Toby Inkster wrote:
maya wrote:

if (email.indexOf('@') != -1) { // etc..

if (strstr(email, '@')!==FALSE)
{
// etc...
}


thank you.. this is exactly what I was looking for ..
(noticed that it works both with '!==' and '!='.... there's probably
some subtle difference I'm still not aware of....:) thank you very much..
Jun 12 '06 #5
Rik
maya wrote:
thank you.. this is exactly what I was looking for ..
(noticed that it works both with '!==' and '!='.... there's
probably
some subtle difference I'm still not aware of....:) thank you very
much..


!= converted to the same type these values are not the same.
!== these values aren't of the same value OR different types.
!== & === compare types, for instance a boolean with the same value a string
would hold converted to boolean won't match.

http://www.php.net/manual/en/languag...comparison.php

Grtz,
--
Rik Wasmus
Jun 12 '06 #6
You can also use

// Does string contain xyz?
if ( ($spos=strpos("abcxyz","xyz")) >0 ) {
... do code ...
}
maya wrote:
hi, am new to PHP, trying to learn strings... I'm a bit mystifed about
something.. how do you test for conditions in strings? for example,
how would you do what in JavaScript (or Java) you do like this

if (email.indexOf('@') != -1) { // etc..

also how do you test for length of a string? I tried:

if (strlen($myName) == 7) {

but got error...

I have looked at sections dealing with conditionals in tutorials, but
don't see examles of this (also don't see functions like indexOf(),
etc.. under Strings here, http://us2.php.net/manual/en/ref.strings.php..

thank you very much..


Jun 12 '06 #7
On Sun, 11 Jun 2006 15:52:55 -0400, maya wrote:
also how do you test for length of a string? I tried:

if (strlen($myName) == 7) {

but got error...


No errors here. May be you tried with a fake id?

$ php -r '$myName="Mladen"; if (strlen($myName)==6) { echo "Works\n"; }'
Works

--
http://www.mgogala.com

Jun 12 '06 #8
maya wrote:
noticed that it works both with '!==' and '!='.... there's probably
some subtle difference I'm still not aware of.


There is, and it's a fairly important difference with strstr.

if (1==TRUE) // true
if (1===TRUE) // false

'==' (and '!=') test whether two things are (or are not) equal-or-equivalent.

'===' (and '!==') test whether two things are (or are not) exactly-equal.

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

Jun 14 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: FHuang | last post by:
Ok, I'm having some trouble with conditionals, for some reason PHP is screwing them up. First off, here is the code: <?php $username = "fred"; $userdata = "./$username.txt"; $fp =...
15
by: Joshua Ginsberg | last post by:
Is there any plan to include inline conditionals in Python? For example: def isNegative(x): return x < 0 ? True : False Thanks! -jag --
1
by: Paul Dale | last post by:
Hi All, I know that several of you will probably want to reply "you should write a parser", and I may. For that matter any tips on theory in that direction would be appreciated. However, if...
3
by: steven | last post by:
Is it possible to combine conditionals to call out data? I have a list of records that include a date. I can call out all dates after the date after tomorrow: IIf(Date()+1)<,...,... And I can...
4
by: Leon Lambert | last post by:
I would appreciate it if someone could help me understand NaN handling with respect to conditionals in IL code. I am playing with a small IL interpreter and having a little problem with it....
11
by: .Net Sports | last post by:
I need to convert some C# ?Conditionals over to vb.net (most likely in vb.net using If..Then statements), but the C#2VBNETconverter by KamalPatel isn't converting them: string PurchaseType =...
15
by: allthecoolkidshaveone | last post by:
I want to convert a string representation of a number ("1234") to an int, with overflow and underflow checking. Essentially, I'm looking for a strtol() that converts int instead of long. The...
5
by: Hul Tytus | last post by:
comp.lang.c c programs & shell conditionals How is a unix shell script made to respond to the value returned by a program compiled from c code? The shell script below is my current effort,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.