Jump to content

So I really need to figure out what I'm doing wrong and why exactly my if statement is never true, but I can't figure out without getting this to actually print anything:

echo strpos($output, "[servername] => $servername");

But I cannot seem to figure out why it always just prints nothing. I'm very new to PHP, I usually work with Python or Java. Am I just missing something? Is there any way to get it to print "false" or "null" when it can't find the position?

Link to comment
https://linustechtips.com/topic/617684-help-printing-emptiness-in-php/
Share on other sites

Link to post
Share on other sites

Can you explain more what you are trying to do. PHP docs are very good: http://php.net/manual/en/function.strpos.php

CPU: AMD 5950X    MB: Asus ROG Crosshair VIII Dark Hero    RAM: HyperX Predator 64GB    GPU: Nvidia RTX 3090 Ti FE    SSD: Seagate FireCuda 530 2TB    
PSU: EVGA 1200w P2    COOLING: EK AIO Elite 360    CASE: Fractal Design Torrent 
   DISPLAY: LG CX48 4k OLED    AUDIO: HIFIMAN Arya SE

Link to post
Share on other sites

1 minute ago, Kryptyx said:

Can you explain more what you are trying to do.

Ultimately the code will look something like this:

    if (strpos($output, "[servername] => $servername") != false) {

        echo "There is a $serverstatus alert on the $servername server where one of your products is hosted.";
    }

But the strpos function keeps outputting nothing. It doesn't evaluate to false or true or "" or null. So I need to figure out what its outputting in order to figure out how to fix the problem.

Link to post
Share on other sites

Since the $servername variable is in double quotes it will get resolved.

CPU: AMD 5950X    MB: Asus ROG Crosshair VIII Dark Hero    RAM: HyperX Predator 64GB    GPU: Nvidia RTX 3090 Ti FE    SSD: Seagate FireCuda 530 2TB    
PSU: EVGA 1200w P2    COOLING: EK AIO Elite 360    CASE: Fractal Design Torrent 
   DISPLAY: LG CX48 4k OLED    AUDIO: HIFIMAN Arya SE

Link to post
Share on other sites

43 minutes ago, Kryptyx said:

Since the $servername variable is in double quotes it will get resolved.

What do you mean? The variable has to be in double quotes to register as a variable and not just more text, but it won't print either way

Link to post
Share on other sites

Right, just making sure you knew it was getting evaluated.

So to be clear, you can't echo a boolean. You can use var_dump if you want to test the value and print that.

 

Example:

var_dump(strpos($output, "[servername] => $servername"));

 

CPU: AMD 5950X    MB: Asus ROG Crosshair VIII Dark Hero    RAM: HyperX Predator 64GB    GPU: Nvidia RTX 3090 Ti FE    SSD: Seagate FireCuda 530 2TB    
PSU: EVGA 1200w P2    COOLING: EK AIO Elite 360    CASE: Fractal Design Torrent 
   DISPLAY: LG CX48 4k OLED    AUDIO: HIFIMAN Arya SE

Link to post
Share on other sites

The echo statement does not return a truth value. Just use print instead. Print works just as well in PHP

I am good at computer

Spoiler

Motherboard: Gigabyte G1 sniper 3 | CPU: Intel 3770k @5.1Ghz | RAM: 32Gb G.Skill Ripjaws X @1600Mhz | Graphics card: EVGA 980 Ti SC | HDD: Seagate barracuda 3298534883327.74B + Samsung OEM 5400rpm drive + Seatgate barracude 2TB | PSU: Cougar CMX 1200w | CPU cooler: Custom loop

Link to post
Share on other sites

42 minutes ago, Kryptyx said:

Right, just making sure you knew it was getting evaluated.

So to be clear, you can't echo a boolean. You can use var_dump if you want to test the value and print that.

 

Example:


var_dump(strpos($output, "[servername] => $servername"));

 

Thanks, that's exactly what I was looking for. So I figured out it's returning false every time, but it's even returning false where it's looking for a string that I know is contained in the $output variable.

For example:

$servername = "s9";
$thestring = "[servername] => $servername";

var_dump(strpos($output, $thestring));
var_dump(strpos($output, "[servername] => s9"));

The first var_dump returns bool(false), but the second returns the position in the $output variable. From what I can tell, these are exactly the same when just echoed, so is there any reason that it isn't working once I use it with the strpos function?

Link to post
Share on other sites

The following code works, results in `int 17`.
 

$servername = "s9";
$thestring = "[servername] => $servername";
$output = "[hello] => world
[servername] => s9
[linus] => techtips";

var_dump(strpos($output, "[servername] => $servername"));

CPU: AMD 5950X    MB: Asus ROG Crosshair VIII Dark Hero    RAM: HyperX Predator 64GB    GPU: Nvidia RTX 3090 Ti FE    SSD: Seagate FireCuda 530 2TB    
PSU: EVGA 1200w P2    COOLING: EK AIO Elite 360    CASE: Fractal Design Torrent 
   DISPLAY: LG CX48 4k OLED    AUDIO: HIFIMAN Arya SE

Link to post
Share on other sites

4 hours ago, Kryptyx said:

The following code works, results in `int 17`.
 


$servername = "s9";
$thestring = "[servername] => $servername";
$output = "[hello] => world
[servername] => s9
[linus] => techtips";

var_dump(strpos($output, "[servername] => $servername"));

Yeah you're right. So I tested this exact code except with my setup. The only difference is that I'm actually reading the $servername variable out of a txt file. It's on a line by itself and is the only thing printed when I print the specific line, but it doesn't work with the strpos function. I didn't think it mattered that it was being read from a file but that seems to be where the issue originates. I took your code and modified it to this:

$file = "status.txt";
$lines = file($file, FILE_IGNORE_NEW_LINES);

$servername = $lines[1];
$thestring = "[servername] => $servername";
$output = "[hello] => world
[servername] => s9
[linus] => techtips";

echo $servername;
var_dump(strpos($output, "[servername] => $servername"));

It now results int 's9bool(false)'

 

So even though it is still just s9, the strpos now returns false. Maybe you know the cause, but I am somewhat lost.

Link to post
Share on other sites

Also keep in mind that some functions can return FALSE or a value that is evaluated as FALSE by the !=  or == .  If you really want to check against only FALSE (boolean), you have to use !== or ===

See http://php.net/manual/en/language.operators.comparison.php and hopefully it makes sense to you.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×