Page 1 of 1

comparing string in PHP ??

Posted: Mon Sep 04, 2006 12:40 pm
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

Posted: Mon Sep 04, 2006 12:49 pm
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;
}

?>

Posted: Mon Sep 04, 2006 1:43 pm
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.

Posted: Mon Sep 04, 2006 1:58 pm
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.

Posted: Tue Sep 05, 2006 3:06 am
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:

Posted: Tue Sep 05, 2006 7:11 am
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.