I've a strange problem that I faced recently.
I've the following code to tokenize string in php:
$token = strtok($string, "#");
while ($token != false)
{
echo $token;
$token = strtok("#");
}
The simple problem I've got is that I'm parsing file which contains many numbers, so in this case 0 will be read as false. So, parsing can't be completed.
What should I do?
You should use the !== operator, to compare $token to false :
while ($token !== false)
If you read the manual page of strtok(), you'll see the following note (quoting) :
This function may return Boolean
FALSE, but may also return a
non-Boolean value which evaluates to
FALSE, such as 0 or "".
Please read the section on
Booleans for more information.
Use the === operator for
testing the return value of this
function.
Using !== instead of != will make sure there is no type-conversion done.
For instance, 0 == false ; but 0 !== false.
Related
I have such a prolem since I had to change the server provider.
I was porting a few wordpress based sites using the Duplicator plugin. Now wherever in the content of the page I use the tag, it returns an error
Warning: strpos(): Empty needle in /XXX/post.php on line XXXX
It's about the code:
function is_local_attachment($url) {
if (strpos($url, home_url()) === false) {
return false;
}
if (strpos($url, home_url('/? attachment_id =')) ! == false) {
return true;
}
$id = url_to_postid($url);
if ($id) {
$post = get_post($id);
if ('attachment' == $post-> post_type) {
return true;
}
}
return false;
}
and more specifically about the line:
if (strpos($url, home_url()) === false) {
Has anyone had such a case and knows how to solve it?
Everything was fine before the server switch. And I wish it would continue to be the case. I'd rather not fix the bug, but find and eliminate what led to it.
The only thing that comes to mind is whether I didn't have Site Address URL on the previous server, I don't have to have it on the new one - the field is empty.
The easiest way to solve this is to put a "guard clause" in. Something like this:
function is_local_attachment($url) {
$homeUrl = home_url();
if (empty($home_url)) {
return;
}
. . .
I don't know what home_url() returns but obviously it can return an empty string and that is causing you a probelm.
If you are using PHP 7.1 or higher you could use something like:
if (strpos($url, home_url()??' ') === false)
Personally, I wouldn't because it's more difficult to read, but it would solve your problem.
Cheers! :)
=C=
I set cookie with php, using
setcookie("name", "John Mayer", time()+31556926 , "/");
After, I want to search Cookie value with php to check if it includes a value, using
if (strpos($_COOKIE["name"],"Mayer")) {
However, it always returns false. Is it not allowed to check cookie value with php ? If so is there anyway to check it ?
Actually strpos returns a index so,if string matched from initial then it returns 0 which is zero and considered as false. It returns -1 if search text didnot matched, so you can make a condition like-
if (strpos($_COOKIE["name"],"Mayer")>=0)
{
}
// or strict comparison with false
if (strpos($_COOKIE["name"],"Mayer")!==false)
{
}
Change it to:
if (strpos($_COOKIE["name"],"Mayer") !== false) {
PHP strpos manual
You edit your code.
if(isset($_COOKIE["name"]) && !empty($_COOKIE["name"])) {
if (strpos($_COOKIE["name"], 'Mayer') !== false) {
echo 'exists';
}
}
check if its set and proceed
if(isset($_COOKIE["name"])) {
if (strpos($_COOKIE["name"], 'Mayer') !== false) {
// it exists
echo 'true';
}
}
I have two type of links which are strings taken from database:
http://www.website.com/anything-else.html
www.website.com/anything-else.html
I need ALL links to be displayed with http:// no matter what so Im using this simple code to determine whether link has http in it and if not add it:
if (strpos($links, 'http') !== true) {
$linkai = 'http://'.$links;
}
The problem is, it is adding http:// to any link no matter if it has it or not.
I tried ==false ect. Nothing works. Any ideas?
Try this
if (strpos($links, 'http') === false) {
$linkai = 'http://'.$links;
}
In strpos documentation says return value not Boolean always.
"Warning
This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function."
$arrParsedUrl = parse_url($links);
if (!empty($arrParsedUrl['scheme']))
{
// Contains http:// schema
if ($arrParsedUrl['scheme'] === "http")
{
}
// Contains https:// schema
else if ($arrParsedUrl['scheme'] === "https")
{
}
}
// Don't contains http:// or https://
else
{
$links = 'http://'.$links;
}
echo $links;
I have following code in php:
$mode = $this->input->get('mode'); // may return string or boolean false
if(!$mode)
$mode = 'default';
how can shorten this code to one line with fastest execution
Is this perfect one:
$mode = ($mode=$this->input->get('mode'))?$mode:'default';
$mode = $this->input->get('mode') or $mode = "default";
But why bother for something this small?
Please note that the actual result of evaluating the above expression is true but that expression is not assigned to anything. If we were to do $something = ($mode = $this->input->get('mode') or $mode = "default"); we would get true in $something. To make it clearer what I wrote above is evaluated like this:
($mode = $this->input->get('mode')) or ($mode = "default");
I don't think you could win anything by this premature optimization except a line of source:
if(!($mode = $this->input->get('mode'))) {
$mode = 'default';
}
A more readable way.
if(!($mode = $this->input->get('mode')))
$mode = 'default';
And a less readable way.
$mode = ($mode = $this->input->get('mode'))?$mode:'default';
If any falsy value (e.g. null, empty string, 0) should be set to 'default', then this will do the job:
$mode = ($this->input->get('mode') || 'default');
This returns only boolean values, sorry.
$mode = !empty($_GET['mode']) ? $this->input->get('mode') : 'default';
As your probably doing some sanitization within your get method then instead of using that to check if a variable is set, you can use !empty on the $_GET, as your only checking if isset theres no security issue what so ever, so this will check if the string is actually set, aswell as check if its empty.
if this result's to true, it will then fetch the sanitized version from your get method and assign it to $mode, otherwise will assign default to $mode.
Hope this helps.
Not exactly in one line, but nevertheless.
if ( false === $mode = $this->input->get('mode') )
{
$mode = 'default';
}
Ternary operations
$mode = ($this->input->get('mode')) ? $this->input->get('mode') : 'default';
EDIT:
If $this->input->get() operates at anything greater than O(n) don't use this.
now i make simple system in my site and this its code
if(stc($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST'])){
//download directly
}else{
//open page first
}
function stc($haystack, $needle, $offset=0) {
return strpos(strtoupper($haystack), strtoupper($needle), $offset);
}
if any one enterd the link from my site its download directly ok
and if from any other sites its open a page
now its working great
but if any one installed any downloader such as inernet download manager
its make the link directly not going to page first i think because its make HTTP_REFERER null
now how i can doing the system like rapidshare.com
strpos can return 0.... 0 as in the pos a needle can be (at the first postion in the haystack, first position in a string is 0).
strpos() return's false if the needle was not found.
0 & false are both the same in if statements...
$test = 0;
if (!$test) echo "0 is like false";
$test2 = false;
if (!$test2) echo "This one is also false..";
You have to make sure it's false or 0, this you can do with: ===
if (strpos() === false) echo "It's not found for sure!";
So your code becomes:
if(stc($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST']) !== false){
//download directly
}else{
//open page first
}