/    Sign up×
Community /Pin to ProfileBookmark

Do You Quote or No Quote ?

Folks,

Which your preferred way out of these ?
urlencode(‘string’);
urlencode(“string”);

urlencode($var);
urlencode(“$var”);

Note the sngl quotes, dbl quotes, no quotes.

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@developer_webauthorMay 21.2021 — Folks,

Again, which you prefer out of the two ?

Note the values of both samples' $file_path. Note the '/' at the end on both.

Note the $url on both samples too.
<i>
</i>&lt;?php
//NOTE: Use rawurlencode() for File Path Strings. Use before the '?'.
//NOTE: Use urlencode() for Query Strings. Use after the '?'.

$max = 10; $page = 1;
$file_path = $_SERVER['PHP_SELF'] .'/' .'search';
$query_string = '?tbl=' .urlencode('links') .'&amp;col=' .urlencode('keyword') .'&amp;max=' .urlencode("$max") .'&amp;page=' .urlencode("$page");

$url = rawurlencode("$file_path") .'/' .$query_string;

?&gt;



<i>
</i>&lt;?php
//NOTE: Use rawurlencode() for File Path Strings. Use before the '?'.
//NOTE: Use urlencode() for Query Strings. Use after the '?'.

$max = 10; $page = 1;
$file_path = $_SERVER['PHP_SELF'] .'/' .'search' .'/';
$query_string = '?tbl=' .urlencode('links') .'&amp;col=' .urlencode('keyword') .'&amp;max=' .urlencode($max) .'&amp;page=' .urlencode($page);

$url = rawurlencode($file_path) .$query_string;

?&gt;
Copy linkTweet thisAlerts:
@NogDogMay 21.2021 — > @developer_web#1631949 urlencode("$var");

The quotes in this example serve no purpose. Otherwise I tend to prefer single quotes unless I need the capabilities of double quotes (variable interpolation or special escape characters like "n" or"t").
Copy linkTweet thisAlerts:
@developer_webauthorMay 23.2021 — @NogDog#1631960

If I don't quote non-variables inside urlencode() then I got undefined constant or whatever error 2 days ago.

I have noticed $vars don't need quoting inside urlencode().

My findings correct ?
Copy linkTweet thisAlerts:
@developer_webauthorMay 23.2021 — @NogDog#1631960

Ok. So these 4 are ok, according to you:

**SET 1**

> SAMPLE 1

<i>
</i>&lt;?php

$max = 10; $page = 1;
$file_path = $_SERVER['PHP_SELF'] .'/' .'search'; //COMPARE THIS LINE WITH OTHER SET SAMPLE.
$query_string = '?tbl=' .urlencode('links') .'&amp;col=' .urlencode('keyword') .'&amp;max=' .urlencode($max) .'&amp;page=' .urlencode($page);

$url = rawurlencode($file_path) .'/' .$query_string; //COMPARE THIS LINE WITH OTHER SET SAMPLE.

?&gt;


**SET 1**

> SAMPLE 2

<i>
</i>&lt;?php

$max = 10; $page = 1;
$file_path = $_SERVER['PHP_SELF'] .'/' .'search'; //COMPARE THIS LINE WITH OTHER SET SAMPLE.
$query_string = '?tbl=' .urlencode("links") .'&amp;col=' .urlencode("keyword") .'&amp;max=' .urlencode($max) .'&amp;page=' .urlencode($page);

$url = rawurlencode($file_path) .'/' .$query_string; //COMPARE THIS LINE WITH OTHER SET SAMPLE.

?&gt;



-------


**SET 2**

> SAMPLE 1

<i>
</i>&lt;?php

$max = 10; $page = 1;
$file_path = $_SERVER['PHP_SELF'] .'/' .'search' .'/'; //COMPARE THIS LINE WITH OTHER SET SAMPLE.
$query_string = '?tbl=' .urlencode('links') .'&amp;col=' .urlencode('keyword') .'&amp;max=' .urlencode($max) .'&amp;page=' .urlencode($page);

$url = rawurlencode($file_path) .$query_string; //COMPARE THIS LINE WITH OTHER SET SAMPLE.

?&gt;


**SET 2**

> SAMPLE 2

<i>
</i>&lt;?php

$max = 10; $page = 1;
$file_path = $_SERVER['PHP_SELF'] .'/' .'search' .'/'; //COMPARE THIS LINE WITH OTHER SET SAMPLE.
$query_string = '?tbl=' .urlencode("links") .'&amp;col=' .urlencode("keyword") .'&amp;max=' .urlencode($max) .'&amp;page=' .urlencode($page);

$url = rawurlencode($file_path) .$query_string; //COMPARE THIS LINE WITH OTHER SET SAMPLE.

?&gt;


Am I correct you like: SET 1 Sample 1.

And like: Set 2 Sample 1.

But out of these 2 which one you prefer over the other ?

I prefer: Set 2 Sample 1.

What-about you ?

**Note the code comments. Important.**
Copy linkTweet thisAlerts:
@developer_webauthorMay 23.2021 — @sempervivum

I'd like your feed-back on my previous post, too.

**Note the code comments. Important.**
Copy linkTweet thisAlerts:
@NogDogMay 23.2021 — Too much to read, but this is needless use of concatenation:
[code=php]$file_path = $_SERVER['PHP_SELF'] .'/' .'search' .'/';[/code]
Either of these will work:
[code=php]
$file_path = $_SERVER['PHP_SELF'] .'/search/';
$file_path = "{$_SERVER['PHP_SELF']}/search/";
[/code]
Copy linkTweet thisAlerts:
@developer_webauthorJun 05.2021 — @NogDog#1632051

Thanks. But why the brackets on your 2nd example ?
Copy linkTweet thisAlerts:
@NogDogJun 05.2021 — > @developer_web#1632583 why the brackets on your 2nd example ?

Because that's how you can embed PHP variables within a double-quoted string (or a "HEREDOC"* string), regardless of whether it's an array element or object property, or if it would be unclear where the variable name ends. See [the manual](https://www.php.net/manual/en/language.types.string.php#language.types.string.parsing.complex) for details.

_______________________

* [PHP manual entry on HERDOC](https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc)
×

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 6.16,
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: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

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