Why is '==' not working in PHP? - php

Both values are '2' but I am not getting a true. Why?
echo $getuser. "<br />";
echo $userurl. "<br />";
if ($getuser == $userurl) {
echo "true <br />";
}
Result
2
2

Instead of echoing them out, use var_dump() to see exactly what is stored in those variables:
var_dump($getuser); echo "<br />";
var_dump($userurl); echo "<br />";

You probably have some stray spaces or other characters which are not easy, or maybe not possible to visually detect.
use
var_dump($getuser);
var_dump($userurl);
Pay attention to the string length. Consider using trim() if needed

may be contain dummy space in that, use like this
echo $getuser. "<br />";
echo $userurl. "<br />";
if (trim($getuser) == trim($userurl)) {
echo "true <br />";
}

When I try
$getuser = 2;
$userurl = 2;
echo $getuser. "<br />";
echo $userurl. "<br />";
if ($getuser == $userurl) {
echo "true <br />";
}
Results:
2
2
true
Works for me. On XAMPP (Windows) and Linux (Apache) alike. So there's problem probably somewhere in configuration or something...

Related

PHP regex number formatting

My regex strips all points away except the last one. Great!
But what I wish, is only to strip "thousand" separators, not the "float" ones. I suppose floats ends with one or two digits.
I cannot change the input format.
Any idea how to extend that regex?
<?php
print_r(preg_replace('/\.(?=.*\.)/', '', "1.234.20")); // works
echo "<hr />";
print_r(preg_replace('/\.(?=.*\.)/', '', "1234.20")); // works
echo "<hr />";
print_r(preg_replace('/\.(?=.*\.)/', '', "1234.2")); // works
echo "<hr />";
print_r(preg_replace('/\.(?=.*\.)/', '', "1234.217")); // works
echo "<hr />";
print_r(preg_replace('/\.(?=.*\.)/', '', "1.000.234.20")); // works
echo "<hr />";
print_r(preg_replace('/\.(?=.*\.)/', '', "1.234")); // buuuh, 1234 would be correct
?>
If you assume every thousand separator is followed by exactly 3 digits, you can do it this way.
This excludes the case where a float is ending with 3 or more digits...
<?php
print_r(preg_replace('/\.([0-9]{3})/', '$1', "1.234.20")); // 1234.20
echo "<hr />";
print_r(preg_replace('/\.([0-9]{3})/', '$1', "1234.20")); // 1234.20
echo "<hr />";
print_r(preg_replace('/\.([0-9]{3})/', '$1', "1234.2")); // 1234.2
echo "<hr />";
print_r(preg_replace('/\.([0-9]{3})/', '$1', "1234.217")); // 1234217
echo "<hr />";
print_r(preg_replace('/\.([0-9]{3})/', '$1', "1.000.234.20")); // 1000234.20
echo "<hr />";
print_r(preg_replace('/\.([0-9]{3})/', '$1', "1.234")); // 1234
?>

Getting data from a multidimentional array (Json)

How do I echo the 4 at the bottom?
$request_body = '{"id":8801236,"order_id":"1854071","accepted":true,"type":"Payment","text_on_statement":null,"branding_id":null,"variables":{},"currency":"USD","state":"pending","operations":
[{"id":1,"type":"authorize","amount":8996,"pending":false,"qp_status_code":"20000","qp_status_msg":"Approved","aq_status_code":"000","aq_status_msg":"Approved","data":
{},"callback_url":"http://www.mywebsite/callback.php","callback_success":true,"callback_response_code":"200","created_at":"2015-11-11T13:32:22+00:00"},
{"id":2,"type":"capture","amount":8863,"pending":true,"qp_status_code":null,"qp_status_msg":null,"aq_status_code":null,"aq_status_msg":null,"data":
{},"callback_url":null,"callback_success":null,"callback_response_code":null,"created_at":"2015-11-11T14:37:18+00:00"}],"metadata":
{"type":"card","brand":"visa","last4":"0008","exp_month":1,"exp_year":2019,"country":"US","is_3d_secure":false,"hash":"fdsfsdfsdf4ds65f4dsf65ds4"
,"number":null,"customer_ip":"8.1.1.21","customer_country":"US","fraud_suspected":false,"fraud_remarks":
[]},"link":null,"shipping_address":null,"invoice_address":null,"test_mode":true,"acquirer":"nets","facilitator":null,"created_at":"2015-11-11T13:32:13Z","balance":0}';
$request_array = json_decode($request_body, TRUE);
echo $request_array['qp_status_code']."<br />";
echo $request_array['qp_status_msg']."<br />";
echo $request_array['aq_status_code']."<br />";
echo $request_array['aq_status_msg']."<br />";
I have tried to do a print_r on the request_array, but honestly that only confuses me more. I simply can not see what array these variable lies within. I have tried to call them with both variables and operations but alas.
Your JSON feed has more than one "operations" which means you can call them like this (to get the first only):
echo $request_array['operations'][0]['qp_status_code']."<br />";
echo $request_array['operations'][0]['qp_status_msg']."<br />";
echo $request_array['operations'][0]['aq_status_code']."<br />";
echo $request_array['operations'][0]['aq_status_msg']."<br />";
Or if you need all of them you need to loop in it:
foreach ($request_array['operations'] as $operation) {
echo $operation['aq_status_msg']."<br />";
}

Foreach Loop grabbing JSON data

Could someone assist me in helping me display more than 1/2 results?
Here is my code:
$url = "http://otter.topsy.com/search.json?q=debt%20management&window=a&perpage=10";
$jsonfile = file_get_contents($url);
$obj = json_decode($jsonfile);
foreach($obj as $result) {
echo $obj->response->list[0]->trackback_permalink;
echo "<br />";
echo $obj->response->list[0]->trackback_author_nick;
echo "<br />";
echo $obj->response->list[0]->content;
echo "<br /><br />";
}
?>
*Note: I have taken out my API key.
Using that code it shows two of the same results.
Anyone got a solution?
You iterate over $obj which is the top-level object containing two elements (request and response). Since you probably want to iterate over the response list, this is what you need:
foreach($obj->response->list as $result) {
echo $result->trackback_permalink;
echo "<br />";
echo $result->trackback_author_nick;
echo "<br />";
echo $result->content;
echo "<br /><br />";
}
Ah, just saw it:
remove the $obj++ ! You increment twice during each loop run. Once by the foreach() loop iterating itself, and once by manually iterating.

HTML img in PHP variable

Im trying to do this
$input = "<img src="HTML/images/user.png" alt="" />";
but it does not work out,
i know im supposed to put a / before a " or something please help
Try this
$input = "<img src='HTML/images/user.png' alt='' />";
Or
$input = "<img src=\"HTML/images/user.png\" alt=\"\" />";
all you need to do is escape double quote.
just like below.
\"HTML/images/user.png\" alt=\"\"
Note that you have use " for PHP already.
You can either choose to use ' or \".
For example,
$input = "<img src='HTML/images/user.png' alt='' />";
$input = "<img src=\"HTML/images/user.png\" alt=\"\" />";
Try this
$input = "<img src='HTML/images/user.png' alt='' />";

How do you echo more than one thing with line break tags?

I am currently using the below code to echo a few different variables and 2 line breaks.
But what I would like to know is how can I echo all of the variables including line breaks into one line of code?
<?php
function findworld($var) {
return strpos($var, "world");
}
$firstvar = "hello world";
$secondvar = findworld($firstvar);
$thirdvar = strlen($firstvar);
echo $firstvar;
echo "<br />";
echo $secondvar;
echo "<br />";
echo $thirdvar;
?>
the concat operator in php is "."
echo $firstvar . "<br />" . $secondvar . "<br />" . $thirdvar;
http://www.php.net/manual/en/language.operators.string.php
You can pass multiple parameters to echo, separated by a comma:
echo $firstvar, "<br />", $secondvar, "<br />", $thirdvar;
To avoid repeating the line break, you could also use implode:
$firstvar = "hello world";
$values = array($firstvar,
findworld($firstvar),
strlen($firstvar));
echo implode('<br />', $values);
You can use string concatenation:
echo $firstvar . "<br />" . $secondvar . "<br />" . $thirdvar;
Like others have said, but with speech marks in the all the correct places ;)
echo $firstvar.'<br />'.$secondvar.'<br />'.$thirdvar;
You don't need to concatenate at all with double quotes, you can just:
echo "$firstvar<br />$secondvar<br />$thirdvar";

Categories