This question is directly related to this SO question I posed about 15 minutes ago. This is not intended to be a duplicate post of that one, rather hoping this is more of a learning experience.
In the question posed, I was trying to figure out what was causing a preg_match no deliminator found error. The code (which is not mine) is available here. The error ended up being a missing ‘/i’ in the Windows 3.11 array.
It is important to note, for the sake of this question, that I do not use the browser section.
The first question, and rightfully so, posed what what is the value in $regex. However, I am still unable to find a way to get that value.
What I originally tried was declaring a global variable outside the function, and right before the preg_match line setting the global variable equal to regex. I then attempted to echo the global variable. I was greeted with a blank page.
I should also mention that I had (at the time of asking the original question) removed this code:
$user_agent = getBrowserOS();
$device_details = "<strong>Browser: </strong>".$user_agent['browser']."<br /> <strong>Operating System: </strong>".$user_agent['os_platform']."";
print_r($device_details);
echo("<br /><br /><br />".$_SERVER['HTTP_USER_AGENT']."");
And thus the only thing I should have seen was the value of the global variable. Instead, I saw nothing.
Can someone explain how this should have been debugged?
Edit: As Per John C’s Answer
<?php
$user_agent = $_SERVER['HTTP_USER_AGENT'];
function getOS() {
global $user_agent;
$os_platform = "Unknown OS Platform";
$os_array = array(
'/windows nt 6.2/i' => 'Windows 8',
'/windows nt 6.1/i' => 'Windows 7',
'/windows nt 6.0/i' => 'Windows Vista',
'/windows nt 5.2/i' => 'Windows Server 2003/XP x64',
'/windows nt 5.1/i' => 'Windows XP',
'/windows xp/i' => 'Windows XP',
'/windows nt 5.0/i' => 'Windows 2000',
'/windows me/i' => 'Windows ME',
'/win98/i' => 'Windows 98',
'/win95/' => 'Windows 95',
'/win16' => 'Windows 3.11',
'/macintosh|mac os x/i' => 'Mac OS X',
'/mac_powerpc/i' => 'Mac OS 9',
'/linux/i' => 'Linux',
'/ubuntu/i' => 'Ubuntu',
'/iphone/i' => 'iPhone',
'/ipod/i' => 'iPod',
'/ipad/i' => 'iPad',
'/android/i' => 'Android',
'/blackberry/i' => 'BlackBerry',
'/webos/i' => 'Mobile'
);
foreach ($os_array as $regex => $value) {
var_dump($regex);
var_dump($value);
if (preg_match($regex, $user_agent)) {
$os_platform = $value;
}
}
return $os_platform;
}
?>
I’ll try to focus on this instance rather than general debugging, since I’m sure there are better guides to get you started.
1. Look at the error message.
Most of the time, PHP isn’t too bad at telling you what went wrong and where (within a couple of lines anway). In this instance, the you would have something along the lines of
So the problem is a missing slash on line 36. Line 36 ( which falls within
getOS()):We can infer this is a problem with $regex – in this instance a quick look at the array in question shows a missing
/in one of the lines, but maybe the data is in a database or can’t be so easily checked.2. Start dumping variables
If something isn’t right, checking in your variables is good start. You want to do this as close as possible to your error so you know nothing is happening in between. We know the problem is
$regexfrom the above, so immediately above the problem line:New result:
PHP halts as soon as we get the error, so the last dumped line will show the problem variable. Hello, a regex with a missing slash!
On your approach:
PHP will halt when it hits an error, which is part of why I suggest dumping variables as close to the problem as possible. In your case, the script never got as far as your echo statements.
Bonus notes!
var_dump()for checking variables because it is better at showing an empty/null variable (echo will just print a blank) plus it handles arrays and objects.