But why?
if ('i' == 'і')
echo 'good';
else
echo 'bad';
echos:
>> bad
You should copy this snippet. If you write it by hand, it will work.
It drives me crazy.
You are sneaky! The second I is not a lower case latin small i. I hexdumped it:
hexdump -C check
00000000 69 66 20 28 27 69 27 20 3d 3d 20 27 d1 96 27 29 |if ('i' == '..')|
00000010 0a 20 20 20 20 65 63 68 6f 20 27 67 6f 6f 64 27 |. echo 'good'|
00000020 3b 0a 65 6c 73 65 0a 20 20 20 20 65 63 68 6f 20 |;.else. echo |
00000030 27 62 61 64 27 3b 20 20 0a 0a |'bad'; ..|
0000003a
I'll let you look up D1 96 :-) Awesome tricksy riddle. +1
Delete the code and retype it :-)
There is an extra character or looks-alike nonsense in there (the 'i' == 'i' bit).
With a copy'n'paste -- "bad"
With the line replaced -- "good"
Another way to prove ('i' != 'і') visually!!
http://jsfiddle.net/naeDE/1/
<pre style="font-size:700%">
if ('i' == 'і')
echo 'good';
else
echo 'bad';
</pre>
Related
I have gps tracker which is sending data to my server over tcp and my server has been coded in PHP. the device is sending data in every 5 seconds. The issue is that the first response comes fine as soon as socket established connection with the client but then subsequent responses comes in weird characters and no idea of how to interpret them.
Server.php code
SocketServer.class.php
<?php
require_once("SocketServer.class.php");
$server = new SocketServer("172.xx.xx.xxx",8000);
$server->max_clients = 10;
$server->hook("CONNECT","handle_connect"); // Run handle_connect every time someone connects
$server->hook("INPUT","handle_input");
$server->infinite_loop();
function handle_connect(&$server,&$client,$input) {
SocketServer::socket_write_smart($client->socket,"String? ","");
}
function handle_input(&$server,&$client,$input) {
$trim = trim($input);
if(strtolower($trim) == "quit")
{
SocketServer::socket_write_smart($client->socket,"Oh... Goodbye...");
$server->disconnect($client->server_clients_index); // Disconnect this client.
return;
}
$output = strrev($trim);
SocketServer::socket_write_smart($client->socket,$output);
SocketServer::socket_write_smart($client->socket,"String? ","");
}
Output:
Triggering Hook 'handle_connect' for 'CONNECT'
*HQ,9176952312,V6,115939,V,3127.2665,N,07307.6974,E,0.00,126.00,100822,FFF7FBFF,410,1,325,14688,8992300001243970980F,#
Triggering Hook 'handle_input' for 'INPUT'
0#119.160.59.240 --> $?v?#uY"1'&e0v?
&????ʧ?E9`,
Triggering Hook 'handle_input' for 'INPUT'
0#119.160.59.240 --> $?v?#uY"1'&e0v?
&????ʧ?E9`,
hex dump if data comes correctly
<Buffer 2a 48 51 2c 39 31 37 36 39 35 32 33 37 35 2c 56 36 2c 31 35 35 37 32 32 2c 56 2c 33 31 32 37 2e 33 30 33 34 2c 4e 2c 37 33 30 37 2e 36 36 33 36 2c 45 ... 65 more bytes>
Hex dump incorrect response
24 91 76 95 23 75 15 57 24 10 08 22 31 27 30 34 04 07 30 76 63 0c 00 00 00 ff f7 fb ff 00 01 c7 f0 00 00 00 00 01 9a 01 01 45 7f 2d 00
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 4 years ago.
I can't seem to find why I'm getting a
Parse error: syntax error, unexpected 'if' (T_IF) in /Applications/XAMPP/xamppfiles/htdocs/oop/index.php on line 8
I checked for missing semi-colons/parentheses/curly braces but can't find anything! There are a few more other files these are referring to. Should I post them as well?
(getInstance is a static method in my db class)
(count and get are methods in my db class)
Thanks in advance!
<?php
require_once 'core/init.php';
$user = DB::getInstance()->get('users', array('username', '=', 'bob'));
if(!$user->count()) {
echo 'No user';
} else {
echo 'OK';
}
?>
init.php looks like this:
<?php
session_start();
$GLOBALS['config'] = array(
'mysql' => array(
'host' => '127.0.0.1',
'username' => 'root',
'password' => '',
'db' => 'oop'),
'remember' => array(
'cookie_name' => 'hash',
'cookie_expiry' => 604800),
'session' => array(
'session_name' => 'user')
);
spl_autoload_register(function($class) {
require_once 'classes/' . $class . '.php';
});
require_once 'functions/sanitize.php';
?>
Your file contains the byte sequence 0xefbbbf (which happens to be the UTF-8 encoding of U+FEFF, the zero-width non-breaking space) at the end of line 6:
$ hexdump -C index.txt
00000000 3c 3f 70 68 70 0a 0a 72 65 71 75 69 72 65 5f 6f |<?php..require_o|
00000010 6e 63 65 20 27 63 6f 72 65 2f 69 6e 69 74 2e 70 |nce 'core/init.p|
00000020 68 70 27 3b 0a 0a 24 75 73 65 72 20 3d 20 44 42 |hp';..$user = DB|
00000030 3a 3a 67 65 74 49 6e 73 74 61 6e 63 65 28 29 3b |::getInstance();|
00000040 0a 24 75 73 65 72 2d 3e 67 65 74 28 27 75 73 65 |.$user->get('use|
00000050 72 73 27 2c 20 61 72 72 61 79 28 27 75 73 65 72 |rs', array('user|
00000060 6e 61 6d 65 27 2c 20 27 3d 27 2c 20 27 62 6f 62 |name', '=', 'bob|
00000070 27 29 29 3b ef bb bf 0a 0a 69 66 28 21 24 75 73 |'));.....if(!$us|
00000080 65 72 2d 3e 63 6f 75 6e 74 28 29 29 20 7b 0a 09 |er->count()) {..|
00000090 65 63 68 6f 20 27 4e 6f 20 75 73 65 72 27 3b 0a |echo 'No user';.|
000000a0 7d 20 65 6c 73 65 20 7b 0a 09 65 63 68 6f 20 27 |} else {..echo '|
000000b0 4f 4b 27 3b 0a 7d 0a 0a 3f 3e |OK';.}..?>|
000000ba
Most likely your text editor erroneously added such a non-breaking space because it didn't understand that you were working with code.
PHP happily parses it as the name of a(n undefined) constant, and consequently complains when it then encounters an if construct (which is syntactically invalid immediately after a constant).
Remove this non-breaking space from your file.
I use the following test program to retrieve a website's content:
<?php
function getData( $url, $output ) {
// set the path for CURL
if (file_exists( '/var/lib'))
$curl = 'curl';
else
$curl = 'curl.exe';
$curl .= ' --trace trace.txt --header "User-Agent: Some-Agent/1.0" ';
echo "\nreading $url...\n";
$buffer = shell_exec( "$curl -i \"$url\"" );
// if this is a 301 redirection URL, follow it one step
if ((preg_match( '~^HTTP.+? 301 ~', $buffer )) and preg_match( '~Location: (.+)~', $buffer, $location )) {
$cmd = "$curl -i \"$location[1]\"";
echo "$cmd\n";
$buffer = shell_exec( $cmd );
}
file_put_contents( $output, $buffer );
}
// test with a URL that will be redirected:
getData( "http://www.onvista.de/aktien/fundamental/EISEN-UND-HUETTENWERKE-AG-Aktie-DE0006055007", "DE0006055007-AG.html" );
On my windows machine, this code runs fine. On a linux machine it returns a 500 internal server error.
This is the start of the trace file trace.txt:
== Info: About to connect() to www.onvista.de port 80 (#0)<br>
== Info: Trying 217.11.205.10... == Info: connected<br>
== Info: Connected to www.onvista.de (217.11.205.10) port 80 (#0)<br>
=> Send header, 130 bytes (0x82)<br>
0000: 47 45 54 20 2f 61 6b 74 69 65 6e 2f 66 75 6e 64 GET /aktien/fund<br>
0010: 61 6d 65 6e 74 61 6c 2f 31 53 54 2d 52 45 44 2d amental/1ST-RED-<br>
0020: 41 47 2d 41 6b 74 69 65 2d 44 45 30 30 30 36 30 AG-Aktie-DE00060<br>
0030: 35 35 30 30 37 0d 20 48 54 54 50 2f 31 2e 31 0d 55007. HTTP/1.1.<br>
0040: 0a 48 6f 73 74 3a 20 77 77 77 2e 6f 6e 76 69 73 .Host: www.onvis<br>
0050: 74 61 2e 64 65 0d 0a 41 63 63 65 70 74 3a 20 2a ta.de..Accept: *<br>
0060: 2f 2a 0d 0a 55 73 65 72 2d 41 67 65 6e 74 3a 20 /*..User-Agent: <br>
0070: 53 6f 6d 65 2d 41 67 65 6e 74 2f 31 2e 30 0d 0a Some-Agent/1.0..<br>
0080: 0d 0a ..<br>
<= Recv header, 36 bytes (0x24)<br>
0000: 48 54 54 50 2f 31 2e 31 20 35 30 30 20 49 6e 74 HTTP/1.1 500 Int<br>
0010: 65 72 6e 61 6c 20 53 65 72 76 65 72 20 45 72 72 ernal Server Err<br>
0020: 6f 72 0d 0a <br>
The only difference between the windows trace and this one is a CR character after the filename (ending in DE0006055007. How did get there and how can I suppress it? (And no, I don't want to use the PHP cURL module which leads to other problems.)
The http headers you get end with \r\n, as should. It seems curl on linux converts them to \n if it detects the output is tty, but try to redirect to a file and you'll see the \rs in there.
. in preg_match matches also \r character, so it becomes part of $location[1]. Simple solution is to trim it.
This doesn't happen on windows only because you can execute curl -i "http://google.com. The quotation is ended by the shell automaticaly after newline.
And you should really use escapeshellarg.
I've read and tried every answer here, yet they seem to only apply to strings in non-binary formats. I'm trying to compare differences in binary files and return those in format such as this:
[file1]
-0001010: ac 0f 00 00 01 00 00 00 48 65 6c 6c 6f 2c 20 77 ........Hello, w
[file2]
+0001010: ac 0f 00 00 01 00 00 00 48 75 6c 6c 6f 2c 20 77 ........Hullo, w
xdiff works fine for creating bdiff patches and patching file - I'm looking too illustrate the differences.
$one = 'one'; // original file
$two = 'two'; // updated file
$pat = 'dif'; // bdiff patch
$new = 'new'; // new destfile
xdiff_file_diff_binary($one, $two, $pat);
xdiff_file_patch_binary($one, $pat, $new);
$diff = xdiff_file_diff($one, $two, 1);
if (is_file($diff)) {
echo "Differences:\n"; // result = 1
echo $diff;
}
Maybe xdiff isn't the right extension to be using for this? I'm not sure.
Sound like a big pain in the butt to do in PHP, might I suggest the following bash one-liner?
diff <(hexdump -C file1) <(hexdump -C file2)
Output:
10c10
< 00000090 35 35 61 34 32 62 62 31 30 33 31 62 38 38 39 34 |55a42bb1031b8894|
---
> 00000090 35 35 61 34 32 62 62 31 30 33 31 61 38 38 39 34 |55a42bb1031a8894|
And you can always mess with the options for diff and hexdump.
I am trying to convert strings to numbers using an array with keys as a lookup table.
This is the array:
$q2_10_lt = array("Full-time worker" => 1
, "Part-time worker" => 2
, "Unemployed, would like to work" => 3
, "Unable to work (chronically ill/mentally handicapped/physically handicapped)" => 4
, "Pensioner/retired" => 5
, "Housewife/husband" => 6
, "Student at university of college (post-matric)" => 7
, "High school learner" => 8
, "Primary school learner" => 9
, "Child attending pre-school/nursery school/crèche/day-mother" => 10
, "Child staying at home" => 11
, "Other" => 12);
The problematic key is "Child attending pre-school/nursery school/crèche/day-mother". This key is not found when using the following code:
$person_tempArr[] = $q2_10_lt[$row["q2_10"]] != null ? $q2_10_lt[$row["q2_10"]] : "12";
$person_tempArr[] = $q2_10_lt[$row["q2_10"]] == null ? $row["q2_10"] : "";
The $row["q2_10"] value is just the different strings taken from MySQL DB.
I should get the number 10 from the first line, but instead I get 12 and the complete string unaltered "Child attending pre-school/nursery school/crèche/day-mother".
This must have something to do with the special character è, but I have not been able to solve it. Any help please.
EDIT 1
After doing a hex dump as suggested, I got the following results
From SQL DB:
43 68 69 6c 64 20 61 74 74 65 6e 64 69 6e 67 20 70 72 65 2d 73 63 68 6f 6f 6c 2f 6e 75 72 73 65 72 79 20 73 63 68 6f 6f 6c 2f 63 72 e8 63 68 65 2f 64 61 79 2d 6d 6f 74 68 65 72
From string in php:
43 68 69 6c 64 20 61 74 74 65 6e 64 69 6e 67 20 70 72 65 2d 73 63 68 6f 6f 6c 2f 6e 75 72 73 65 72 79 20 73 63 68 6f 6f 6c 2f 63 72 c3 a8 63 68 65 2f 64 61 79 2d 6d 6f 74 68 65 72
The difference was "E8" vs "C3A8" or "è" from the DB vs "è" from the php string.
So how can I go about ensuring the php string remains "è"?
The string you get from your database layer is not the same string you use as the key. To fix it, use the same string each time.
Same means here, that the string is the same byte-by-byte. Do a hexdump of the string you get from the database.
Then enter the binary string as key (or at least for the special letter). This will make your code more robust because it will work regardless in which encoding you're saving the PHP file.
Edit: As you compare against the database, the key needs to co-related to the binary sequence in the database:
, "Child attending pre-school/nursery school/cr\xE8che/day-mother" => 10
^^^^
Use the hexadecimal notation to express the bytes you are not able to "type" with your UTF-8 encoded PHP file. The database uses some ISO-8859-1 or similar encoding.
As you can see it is just \x and then the hexcode E8. It works in double-quoted strings in PHP.
I ended up changing my table column encoding to utf8_general_ci and table collation to utf8_general_ci. I also added the following lines of code to my php file:
ini_set('default_charset', 'utf-8');
//set encodig to utf-8
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET 'utf8'");
It is working now, but I could be doing something not recommended?