For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > February 2005 > Comparing strings?









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 Comparing strings?
Rory

2005-02-14, 8:56 pm

I am having the following problem, I can't get this to work?

$string1= $GLOBALS["user"];
if($string1=="test")
{
echo $string1;
}

even though I know that $GLOBALS["user"] is assigned the string "test". I
say I know cos if i do this

die($GLOBALS["user"]);

it will show 'test' on screen? I'm new to PHP so sorry if I am not
explaining myself correctly, but any help is appreciated! My programming
skills aren't that bad so this is driving me crazy. Cheers,
Rory


Andy Hassall

2005-02-14, 8:56 pm

On Mon, 14 Feb 2005 21:47:20 -0000, "Rory" <rorywalsh@ear.ie> wrote:

>I am having the following problem, I can't get this to work?
>
> $string1= $GLOBALS["user"];
> if($string1=="test")
> {
> echo $string1;
> }
>
>even though I know that $GLOBALS["user"] is assigned the string "test". I
>say I know cos if i do this
>
>die($GLOBALS["user"]);
>
>it will show 'test' on screen? I'm new to PHP so sorry if I am not
>explaining myself correctly, but any help is appreciated! My programming
>skills aren't that bad so this is driving me crazy. Cheers,


Trailing or leading space? Use var_dump() instead of print, and use a <pre>
element around the output, this gives a better display of the contents of a
variable.

--
Andy Hassall / <andy@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Rory

2005-02-14, 8:56 pm

Thanks, I am not sure about how to use the <pre> tag but if I use a var_dump
to show the varialbe it displays this
string(4) "test"

Would I be right in assuming that the contents of $string1 is simply "test"?
What do you mean by trailing or leading space? I'm only new to this. Cheers,

Rory.



"Andy Hassall" <andy@andyh.co.uk> wrote in message
news:3g8211pogpt5ov16dop5uu90bmsb91qa09@
4ax.com...
> On Mon, 14 Feb 2005 21:47:20 -0000, "Rory" <rorywalsh@ear.ie> wrote:
>
>
> Trailing or leading space? Use var_dump() instead of print, and use a
> <pre>
> element around the output, this gives a better display of the contents of
> a
> variable.
>
> --
> Andy Hassall / <andy@andyh.co.uk> / <http://www.andyh.co.uk>
> <http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool



Deniz Adrian

2005-02-15, 8:55 am

Rory wrote:
> I am having the following problem, I can't get this to work?
>
> $string1= $GLOBALS["user"];
> if($string1=="test")
> {
> echo $string1;
> }
>
> even though I know that $GLOBALS["user"] is assigned the string "test". I
> say I know cos if i do this
>
> die($GLOBALS["user"]);
>
> it will show 'test' on screen? I'm new to PHP so sorry if I am not
> explaining myself correctly, but any help is appreciated! My programming
> skills aren't that bad so this is driving me crazy. Cheers,
> Rory
>
>

What says

<?php

$string1=$GLOBALS['user'];

echo $string1;

?>

i can say there are no errors in your script. if $GLOBALS['user'] is set
properly, as it seems, you should be able to assign it to some other
var, which should be no problem to compare..

best regards
deniz adrian
Deniz Adrian

2005-02-15, 8:55 am

Rory wrote:

> Would I be right in assuming that the contents of $string1 is simply "test"?
> What do you mean by trailing or leading space? I'm only new to this. Cheers,



" test" <- this is a leading space
"test " <- this is a trailing space

regards
deniz adrian
Rory

2005-02-15, 8:55 am

When I do this and type 'test' into my textarea I get the string 'test'
printed to the screen. Is there any way I can just perform a test to see if
there is anything at all in $GLOBALS["user"];, id there is nothing in it
does it return a NULL? in my script I only want a certain action to take
place is the user has typed something into the textarea and do nothing if
they did not type anything at all. Cheers for the help.

Rory.



"Deniz Adrian" <schriftbild.romeike@t-online.de> wrote in message
news:cuscaa$2v8$01$1@news.t-online.com...
> Rory wrote:
> What says
>
> <?php
>
> $string1=$GLOBALS['user'];
>
> echo $string1;
>
> ?>
>
> i can say there are no errors in your script. if $GLOBALS['user'] is set
> properly, as it seems, you should be able to assign it to some other var,
> which should be no problem to compare..
>
> best regards
> deniz adrian



Deniz Adrian

2005-02-15, 8:55 am

Rory wrote:
> When I do this and type 'test' into my textarea I get the string 'test'
> printed to the screen. Is there any way I can just perform a test to see if
> there is anything at all in $GLOBALS["user"];, id there is nothing in it
> does it return a NULL? in my script I only want a certain action to take
> place is the user has typed something into the textarea and do nothing if
> they did not type anything at all. Cheers for the help.
>
> Rory.



try

if (isset($GLOBALS['user']) AND (!empty($GLOBALS['user'])))
{
// Do something, its filled
}

best regards
deniz adrian
Rory

2005-02-15, 8:55 am

Unfortunately $GLOBALS['user'] never seems to be truly empty, whether i
type in there or not. This test did not work either? Here is the html
part for the form, is there anything wrong that you can see here?


<form method="post" action="index.php">
<p>Changes contributed by:</p>
<textarea name="user" cols="40" rows="1">
</textarea>
<input type= "submit" value="$DONE_BUTTON"/>
</form>


Rory




Deniz Adrian wrote:
> Rory wrote:
>
>
>
>
> try
>
> if (isset($GLOBALS['user']) AND (!empty($GLOBALS['user'])))
> {
> // Do something, its filled
> }
>
> best regards
> deniz adrian

Deniz Adrian

2005-02-15, 8:55 am

Rory wrote:
> Unfortunately $GLOBALS['user'] never seems to be truly empty, whether i
> type in there or not. This test did not work either? Here is the html
> part for the form, is there anything wrong that you can see here?
>
>
> <form method="post" action="index.php">
> <p>Changes contributed by:</p>
> <textarea name="user" cols="40" rows="1">
> </textarea>
> <input type= "submit" value="$DONE_BUTTON"/>
> </form>
>
>
> Rory


You bypass the vars by the POST-method, so why access them by $GLOBALS??
Simply do it like this in your php-script:


if (strlen($_POST['user'])>0) {
// "user" is filled
// do something
}

best regards
deniz adrian
Rory

2005-02-15, 8:55 am

Cheers, I had been using $_POST but I somehow moved on to trying
$GLOBALS. Strlen does the trick, but there is one last problem. There
seems to be a lot of empty white space in my textarea when I load the
page? I have to select everything and hit delete to make sure
$_POST['user'] < 0. I tried setting the default value of the textarea
with value="", but that does not seem to work? Any ideas? Thanks again,
you been a great help,

Rory.

Deniz Adrian wrote:
> Rory wrote:
>
>
>
> You bypass the vars by the POST-method, so why access them by $GLOBALS??
> Simply do it like this in your php-script:
>
>
> if (strlen($_POST['user'])>0) {
> // "user" is filled
> // do something
> }
>
> best regards
> deniz adrian

Deniz Adrian

2005-02-15, 8:55 am

Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.t-online.com 1108464149 05 1577 EarBrjPOMMW9vsr 050215 10:42:29
X-Complaints-To: usenet-abuse@t-online.de
X-ID: XpFF4oZbgeeUgfWxMd7Fv+wUD5JLxlIDY1WWHsPi
UfUTXsUhgheOrY
User-Agent: Mozilla Thunderbird 1.0 (Windows/20041206)
X-Accept-Language: en-us, en
In-Reply-To: <cushf9$9cg$1@domitilla.aioe.org>
Xref: newsfeed-west.nntpserver.com alt.comp.lang.php:41540

That shouldn't be too hard to accomplish. At first you shouldn't put
spaces between <textarea name="user"> and </textarea> because what
stands inbetween is seen as the value.
The other possibility would be using php-function trim() on the
serverside, which removes whotespaces from a string.

best regards
deniz adrian


Rory wrote:[color=darkred]
> Cheers, I had been using $_POST but I somehow moved on to trying
> $GLOBALS. Strlen does the trick, but there is one last problem. There
> seems to be a lot of empty white space in my textarea when I load the
> page? I have to select everything and hit delete to make sure
> $_POST['user'] < 0. I tried setting the default value of the textarea
> with value="", but that does not seem to work? Any ideas? Thanks again,
> you been a great help,
>
> Rory.
>
> Deniz Adrian wrote:
>
Rory

2005-02-15, 8:55 am

Thanks, that's sorted it all out. Thanks again for your time.

Deniz Adrian wrote:
> That shouldn't be too hard to accomplish. At first you shouldn't put
> spaces between <textarea name="user"> and </textarea> because what
> stands inbetween is seen as the value.
> The other possibility would be using php-function trim() on the
> serverside, which removes whotespaces from a string.
>
> best regards
> deniz adrian
>
>
>

Sponsored Links







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

Copyright 2008 codecomments.com