/    Sign up×
Community /Pin to ProfileBookmark

How To Default Page Number To 1 By Writing As Minimum Amount of Code ?

Folks,

I want to check if the page number exists in url or not. If not then by default the page nu8mber should be “1”.
(Imagine a SERP. They usually contain page number in their urls).

Notice my questions in the comments of my two codes. And try answering them.

Is this code ok or faulty ?

“`
if(!empty($_REQUEST[‘page’]) && is_int($_REQUEST[‘page’]))
{
$page_no = strip_tags(stripslashes($_REQUEST[‘page’])); //Is strip_tags() or stripslashes() necessary here ?
}
else
{
//If Page Number not in Url then default the page number to: 1.
$page_no = INT(1); //Which one is correct ?
$page_no = INTVAL(1); //Which one is correct ?
}
“`

How to shorten the code ?

My attempt to shorten it:
Is this code ok or faulty ?

“`
if(!empty($_REQUEST[‘page’]) && is_int($_REQUEST[‘page’]))
{
$page_no = strip_tags(stripslashes($_REQUEST[‘page’])) : $page_no = 1; //Is strip_tags() or stripslashes() necessary here ?
//Is this part valid or not: “: **$page_no = 1;**”
}
else
{
//If Page Number not in Url then default the page number to: 1.
$page_no = INT(1); //Which one is correct ?
$page_no = INTVAL(1); //Which one is correct ?
}
“`

Show us how you would write it.

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJul 01.2021 — [code=php]
$page_no = (!empty($_REQUEST['page']) and intval($_REQUEST['page']) > 0)
? intval($_REQUEST['page']) : 1;
[/code]

Note, there is no reason to cast a literal 1 to an integer via intval(1), since it is already an integer.

See https://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary for info on the ? : "ternary" operator
Copy linkTweet thisAlerts:
@developer_webauthorJul 05.2021 — @NogDog#1633597

Thanks for the link, it helped!
Copy linkTweet thisAlerts:
@developer_webauthorJul 05.2021 — @NogDog#1633597

I'm a little confused on the ternary thing if there is an else involved and you want an error echoed, like so:
<i>
</i>if($col=='submission_id')
{
if(!is_INT($col))
{
die('Enter a Submission Number! Can only contain a number.');
}
}


How to ternary whip this one ?
Copy linkTweet thisAlerts:
@NogDogJul 05.2021 — > @developer_web#1633737 How to ternary whip this one ?

That's a different use case than you implied in the original post. You'd probably have to use an if/else and write more code. I'd recommend putting all of your form input validation into its own, separate section; then if everything is okay, go ahead with the logic/processing to do whatever you need to do with those inputs after they have been validated.
Copy linkTweet thisAlerts:
@developer_webauthorJul 18.2021 — @NogDog#1633597

**>>Note, there is no reason to cast a literal 1 to an integer via intval(1), since it is already an integer.<<**

Oh!

Ok. Then, can you show me when to use the intval() then ? 2-3 sample codes should teach me to strongly understand it.
Copy linkTweet thisAlerts:
@developer_webauthorJul 23.2021 — @NogDog#1633597

**>>Note, there is no reason to cast a literal 1 to an integer via intval(1), since it is already an integer.<<**

Oh!

Ok. Then, can you show me when to use the intval() then ? 2-3 sample codes should teach me to strongly understand it.
Copy linkTweet thisAlerts:
@developer_webauthorJul 23.2021 — @DaveyErwin

Can you show me instead, what I asked NogDog above ?

Cheerio!
×

Success!

Help @developer_web spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...