Hello,

Was reading this tutorial on the INTVAL():
https://www.w3schools.com/php/phptryit.asp?filename=tryphp_func_var_intval

How is this an INT ?

$e = array("red", "green", "blue");
echo intval($e) . "<br>";

And, when to use int() function and when to use INTVAL() function and when not to use them ? I am confused!

intval() is used when you want to convert a variable, no matter what type of variable it is, to an integer.

It can be useful if you are expecting the user to enter a number, but instead they enter a word, and you want to convert whatever they enter to an integer before they use it. For example:

// Retrieve a number from the query string
$expected_integer = $_REQUEST['integer'];

// Add 25 to the number passed in, and print it out
echo 25 + intval($expected_integer);

This prevent users from being able to do things like page.php?integer=foo and then causing errors because PHP can't figure out how to add 25 to the word foo.

PHP doesn't have a function called int() but there is a concept in programming called type casting, which is a native ability of the programming language (not through a function) to convert variables of one type into variables of another type. For example, you can do:

echo 25 + (int)$integer;

As a beginner programmer, there's not really a need to concern yourself with that. I have been doing PHP programming for 20 years and I can count on one hand how many times I've had to use typecasting. Just use intval() to sanitize user input when you're expecting an integer, before you use it.

A good use case for intval() is page navigation. For example, suppose you have document.php?page=1 and document.php?page=2 and document.php?page=3.

You will want to use intval() on the page number before you start using it in an SQL query string to fetch the correct rows from the table, for example, to protect against people trying to go to the URL document.php?page=apple.

commented: 20 Yrs php programming ! You look no older than 26 on your pic! +0

@Dani,

Let me see if I understood you correctly or not.

/* Imagine Url is: https://localhost/Work/buzz/Templates/Pagination_TEMPLATE.php?tbl=links&bool=null&col_1=domain&input_1=brute.com&lmt=1&pg=1
*/

// Retrieve a number from the query string
$expected_integer = $_REQUEST['integer']; 

What would $expected_integer echo here ?
Do not forget there are two/three INTS in the url: &bool=null&lmt=1&pg=1
Let me know if the &bool=null can be counted as an INT here or not since NULL is 0.
Anything else I need to know ?

Thank you!

The $_REQUEST[] array fetches parts of the URL query string. $_REQUEST['integer'] would be null, but $_REQUEST['tbl'] would give you links, $_REQUEST['bool'] would give you null, $_REQUEST['col_1'] would give you domain, etc.

Sorry for the confusion. In my example when I had:

$expected_integer = $_REQUEST['integer'];

it was because I was talking about the URL:

page.php?integer=foo

The 'term' integer has no special meaning other than that was just what I happened to have used in my attempt to point out that an integer was expected by naming the query parameter 'integer' (arbitrary name).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.