PHP Notice: A non well formed numeric value encountered [duplicate] - php

This question already has answers here:
number_format() causes error "A non well formed numeric value encountered"
(5 answers)
Closed 6 years ago.
I write a function like this:-
function loadTime() {
$load = microtime();
return number_format($load,2);
}
and then call it with a piece of HTML code:
Page generated in <?php echo loadTime() ?> seconds.
I think you can guess what i wanna do! i wanna display my page load time with this PHP function, i have to say that this function works but when i open error_log it shows this error:
PHP Notice: A non well formed numeric value encountered in
/home/coffeesc/public_html/index.php on line 12
as I searched, I think something is wrong with time or even date! no idea but I wonder if you can suggest me a way for displaying page load time with php (somehow in ms)

If you use microtime() and set the get_as_float parameter you get a simple floating point number that is just what you need for this kind of calculation
$page_start = microtime(1);
// all page code
sleep(2);
echo 'Page generated in ' . (microtime(1) - $page_start) . ' seconds';
The result being something like this
Page generated in 2.0001142024994 seconds

Related

The right way to do preg_match and preg_replace [duplicate]

This question already has answers here:
PHP: Best way to check if input is a valid number?
(6 answers)
Closed 2 years ago.
So I have been trying to bring back to life my very old website.
I started with replacing ereg with preg, but it's been a very long time since I have written any PHP.
At the moment I am stuck on this:
$_POST['amount'] = preg_replace("/[^0-9/]",'',$_POST['amount']);
$_POST['amount'] = round($_POST['amount']);
if (!preg_match('/[^0-9]/', $_POST['amount'])) {
echo "Invalid amount.";
}else {
echo "Passed";
}
I'm not entirely sure where I am going wrong. Should it be !preg_match or preg_match for example?
Edit:
$_POST['amount'] allows a user to enter a number and needs to replace anything else other than a number if attempted.
Not sure what the error is (i.e. what is the current output and what was expected). However, one mistake in the prompt is that the pattern for the first preg_replace appears to have a typo: /[^0-9/] should probably be /[^0-9]/
preg_replace("/[^0-9]/",'',$_POST['amount']);
The rest looks like correct syntax (i.e. preg_match returns 0 if there is no match).

How to convert output of date("Ymd") into a number in PHP? [duplicate]

This question already has answers here:
How do I convert a string to a number in PHP?
(35 answers)
Closed 5 years ago.
I want to have the desired output as such 20170613 which is an integer.
I know using strtotime() I can get an UNIX timestamp as an integer, but I don't want that. date("Ymd") however returns a string.
I can't seem to figure a way to convert this to an integer.
Edit #1: Here is what I am attempting:
$x = (int)date("Ymd");
echo $x;
The result however does not show up in the browser. Infact in the developer's tools, it shows internal server error.
The term to Google is "type cast". That leads you to the PHP type juggling docs on integer casting.
Taking that as a reference point, the canonical way to go about it is:
$int = (int)date('Ymd');
For completeness, you could also use the equivalent full form:
$int = (integer)date('Ymd');
Or the functional:
$int = intval(date('Ymd'));

Undefined offset when trying to get the next position in php [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 6 years ago.
error message when trying to print the next position in loop?
$coba = "testing";
for($h=1;$h<(count($coba));$h++){
echo $coba[$h+1];
}
If this is your code, you would get that error:
$coba = "testing";
for($h=1;$h<strlen($coba);$h++){
echo $coba[$h+1];
}
The reason is you're trying to print something that don't exist.
You are incrementing $h to a number greater than the array. That's why you are getting the offset error.
Original code is incorrect here --> for($h=1;$h<(**count**($coba));$h++){
See the PHP reference guide for difference in count() and strlen().
count($coba) is 1
strlen($coba) is 7
If you use strlen then the code would correctly loop through the string:
$coba = "testing";
for($h=1;$h<strlen($coba);$h++){
echo $coba[$h+1];
}
Now, regarding the error you mentioned, when I run the above I get:
PHP Notice: Uninitialized string offset: ...
Two problems here with your original code. Since I do not know your original intent, I can only guess at what you were trying to do with the loop.
Problem #1: indexing into the string should start with 0, not 1
Problem #2: $coba[$h+1] will be an invalid index at the END of the array at h+1.
You can either adjust the indexing h+1 to just be h OR
change the loop to loop 1 less via for($h=0;$h<(strlen($coba)-1);$h++){
Thus, final code could look like:
$coba = "testing";
for($h=0;$h<(strlen($coba)-1);$h++){
echo $coba[$h+1];
}
Which outputs when run:
esting

Trying to get property of non-object? [duplicate]

This question already has answers here:
Notice: Trying to get property of non-object error
(3 answers)
Closed 7 years ago.
I've been getting an error message for the following piece of code (I'm trying to get the content inside the 'article' tags on a certain web page):
function getTextFromLink($url) {
$html = new DOMDocument();
$html->loadHTML($url);
$text = $html->getElementsByTagName('article')->item(0)->textContent;
return $text;
}
It says that I'm trying to get the property of a non-object on the line with
$text = $html->getElementsbyTagName('article')->item(0)->textContent;
I'm fairly new to php and DOM; what am I missing here?
You have two problems in your code:
The obvious problem is that $html->getElementsByTagName('article')->item(0) is not an object. Specifically, it is null, since the HTML you're parsing doesn't actually contain any article elements. You could've figured this out yourself by following Devon's advice and viewing the value of $html->getElementsByTagName('article')->item(0) using var_dump().
Now, why doesn't your HTML contain any article elements? Well, the real problem turns out to be that the loadHTML() method will load HTML from a string and parse it. That is to say, when you call $html->loadHTML($url);, PHP will parse the contents of the string variable $url as HTML code, and give you a DOMDocument representing the result. Given that you named the variable $url, I'm pretty sure that's not what you want.
What you actually want to use instead is probably loadHTMLFile(), which actually loads HTML code from a named file (or, apparently, URL), rather than from a PHP string.

Strange offset error in PHP [duplicate]

This question already has answers here:
What causes: "Notice: Uninitialized string offset" to appear? [closed]
(5 answers)
Closed 9 years ago.
I am getting an error I cannot solve.
The error is:
Uninitialized string offset: 1
The code is:
if($play_count_within_45_minutes[1] > $play_history_old_over_45_minutes_ago[$i][1]){...}
The error is occurring on the $play_count_within_45_minutes[1] variable, because when I change the index to 0 (as in $play_count_within_45_minutes[0]), it works fine.
I had it display what the value would be, and it outputs fine, with this code:
print_r($play_count_within_45_minutes[1]);
The output is:
1.0E+80
The original variable declaration is:
$play_count_within_45_minutes = [0, 100000000000000000000000000000000000000000000000000000000000000000000000000000000];
I don't think the number is too large, as I tried changing it 1 and I got the same error.
I found it has something to do with this loop (print_r() is in there as I was testing it, and it gives the same error):
for($i=0; $i <= (count($play_history_old_over_45_minutes_ago)-1); $i++ ){
echo "<br>";
print_r($play_count_within_45_minutes[1]);
echo "<br>";
if($play_count_within_45_minutes[1] > $play_history_old_over_45_minutes_ago[$i][1]){
$play_count_within_45_minutes = $play_history_old_over_45_minutes_ago[$i][1];
}
}
Alright, I found the reason I couldn't figure it out.
What I didn't know about the PHP errors is that it treats everything after the original if statement as the same line, so even though the error was on line 105, it kept telling me it was on 104.
However, I still don't know why changing the original $play_count_within_45_minutes[1] to $play_count_within_45_minutes[0] solved the problem. Nonetheless, that would have still had a problem.
The fixed code is:
for($i=0; $i <= (count($play_history_old_over_45_minutes_ago)-1); $i++ ){
echo "<br>";
print_r($play_count_within_45_minutes[1]);
echo "<br>";
if($play_count_within_45_minutes[1] > $play_history_old_over_45_minutes_ago[$i][1]){
$play_count_within_45_minutes[1] = $play_history_old_over_45_minutes_ago[$i][1];
}
}
It seems like a dumb error, but I really didn't know about PHP treating the entire line following the if statement as the same line.

Categories