comparing string in PHP ??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ansa
Forum Commoner
Posts: 31
Joined: Tue Aug 29, 2006 1:58 am
Location: Hamburg, Germany

comparing string in PHP ??

Post by ansa »

Hi,

I thought it was gonna be simple, but I spent the last hour trying to figure it out without any luck. I want to compare two strings after being input by a user, such as an email and the email verification.

Here is my code using '!=' and '<>' (I tried both):

if ($email_input != $confirm_mail) $error = 1;

After learning that it did not work, then I tried using strcmp:

if (strcmp($email_input,trim($confirm_mail)) != 0) $error = 1;


None of these works. Can anyone make a suggestion?

-Andre
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Both of your attempts are generally fine. Maybe the values aren't correct? What's the rest of your code look like? I'm mainly looking for how these variables are set and the form you are using to submit them.

This may also shed some light: run the following in a new file and tell us the results please.

Code: Select all

<?php

$neg = array('off', 0, false, '', null);
$flags = array(
	'Register Globals' => 'register_globals',
	'Short Tags' => 'short_open_tag',
	'Display Errors' => 'display_errors',
	'Magic Quotes GPC' => 'magic_quotes_gpc',
	'Magic Quotes Runtime' => 'magic_quotes_runtime',
	'Magic Quotes Sybase' => 'magic_quotes_sybase',
);
$ve = phpversion();
$os = PHP_OS;
$er = intval(error_reporting());
foreach ($flags as $n => $v)
{
	$flags[$n] = (in_array(strtolower(ini_get($v)), $neg) ? 'Off' : 'On');
}
$cli = (php_sapi_name() == 'cli');
$eol = "\n";

$gle = get_loaded_extensions();
$rows = array();
$le = '';
$wide = 4;
$j = count($gle);
$pad = $wide - $j % $wide;
$len = max(array_map('strlen', $gle));
$func = create_function('$a', 'return str_pad($a, ' . intval($len) . ');');
$gle = array_map($func, $gle);
for($i = 0; $i < $j; $i += $wide)
{
	$le .= '   ' . implode('   ', array_slice($gle, $i, $wide)) . $eol;
}

$ec = array(
	'E_STRICT' => 2048, 'E_ALL' => 2047, 'E_USER_NOTICE' => 1024,
	'E_USER_WARNING' => 512, 'E_USER_ERROR' => 256, 'E_COMPILE_WARNING' => 128,
	'E_COMPILE_ERROR' => 64, 'E_CORE_WARNING' => 32, 'E_CORE_ERROR' => 16,
	'E_NOTICE' => 8, 'E_PARSE' => 4, 'E_WARNING' => 2, 'E_ERROR' => 1,
);

$e = array();
$t = $er;
foreach ($ec as $n => $v)
{
	if (($t & $v) == $v)
	{
		$e[] = $n;
		$t ^= $v;
	}
}
if (ceil(count($ec) / 2) + 1 < count($e))
{
	$e2 = array();
	foreach ($ec as $n => $v)
	{
		if (!in_array($n, $e) and $n != 'E_ALL')
		{
			$e2[] = $n;
		}
	}
	$er = $er . ' ((E_ALL | E_STRICT) ^ ' . implode(' ^ ', $e2) . '))';
}
else
{
	$er = $er . ' (' . implode(' | ', $e) . ')';
}

if (!$cli)
{
	echo '<html><head><title>quick info</title></head><body><pre>', $eol;
}

echo 'PHP Version: ', $ve, $eol;
echo 'PHP OS: ', $os, $eol;
echo 'Error Reporting: ', $er, $eol;
foreach ($flags as $n => $v)
{
	echo $n, ': ', $v, $eol;
}
echo 'Loaded Extensions:', $eol, $le, $eol;

if (!$cli)
{
	echo '</pre></body></html>', $eol;
}

?>
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post by toasty2 »

You could just do

Code: Select all

echo phpinfo();
Wouldn't that tell you all the stuff feyd's script would and more?

Although, feyd's script does make things easier and more specific, phpinfo generates a pretty big page.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

phpinfo() is large enough that it can be quite overwelming for many people. The script posted involves the common settings involved in many errors people are having and pulls information on the loaded extensions, scraping away a lot of cruft that just isn't all that important for us to know all too often.
ansa
Forum Commoner
Posts: 31
Joined: Tue Aug 29, 2006 1:58 am
Location: Hamburg, Germany

Post by ansa »

Oh my God!

I used two different variables. I used $confirm_email and $confirm_mail !! That costs me 1 hour. Sorry to bother you guys
(':oops:')
Embarassed


However, I use PHP 5.2. Is that actually a positive or negative thing as of right now?

Feyd, after I run your code, I have the following result:

PHP Version: 5.2.0-dev
PHP OS: WINNT
Error Reporting: 6135 ((E_ALL | E_STRICT) ^ E_STRICT ^ E_NOTICE))
Register Globals: Off
Short Tags: On
Display Errors: On
Magic Quotes GPC: On
Magic Quotes Runtime: Off
Magic Quotes Sybase: Off
Loaded Extensions:
bcmath calendar com_dotnet ctype
date ftp hash iconv
odbc pcre Reflection session
libxml standard tokenizer zlib
SimpleXML dom SPL wddx
xml xmlreader xmlwriter apache2handler
mysql


What does this tell us, other than the PHP Version?

Thanks for all your help! :oops: :oops: :oops: :oops: :oops:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The script tells us version and build information, error reporting level, register globals status, short tags status, display errors status, magic quotes statuses and a list of all the extensions you have loaded; all the information we need to help a lot of various problems people have.
Post Reply