Asked  7 Months ago    Answers:  5   Viewed   29 times

I am new to PHP. I am implementing a script and I am puzzled by the following:

$local_rate_filename = $_SERVER['DOCUMENT_ROOT']."/ghjr324l.txt";
$local_rates_file_exists = file_exists($local_rate_filename);

echo $local_rates_file_exists."<br>";

This piece of code displays an empty string, rather than 0 or 1 (or true or false). Why? Documentation seems to indicate that a boolean value is always 0 or 1. What is the logic behind this?

 Answers

61

Be careful when you convert back and forth with boolean, the manual says:

A boolean TRUE value is converted to the string "1". Boolean FALSE is converted to "" (the empty string). This allows conversion back and forth between boolean and string values.

So you need to do a:

echo (int)$local_rates_file_exists."<br>";
Wednesday, March 31, 2021
 
Parfait
answered 7 Months ago
52

The reason it echos first, is because it is called, and afterwards are the strings "concatenated" (more on that in a second):

What you want in ToBeIncludedFile.php is return "World!";, not echo.

Right now, this is what happens:

  • You include the file, which doesn't print anything, this is correct.
  • You do a concatenation of the string "Hello" and the return value of printOut(). That means, first that function is called:
  • printOut() executes and prints "World!", returning nothing.
  • Your main script then concatenates "Hello" with nothing and prints that.
Wednesday, March 31, 2021
 
mozlima
answered 7 Months ago
20

It get's echoed, but you won't see anything on your page as the text will be written within the Javascript-tag which is not displayed by the browser. Look at your page source to verify that the text is really there.

EDIT

Try

if(<?php echo json_encode($re); ?>){
    document.getElementById('hide').style.display = "none"; 
}

This will ensure that your PHP string will be converted into the appropriate Javascript type - in case of strings it'll ensure that the string is enclosed in " and is escaped properly.

EDIT again

When you do the following

<script type="text/javascript"> 
if(<?php echo $re; ?>){
    document.getElementById('hide').style.display = "none"; } 
</script>

this is what is written to the HTML page (that's then interpreted by the browser)

<script type="text/javascript"> 
if(whatever is in the $re vairable){
    document.getElementById('hide').style.display = "none"; } 
</script>

But this is not even valid Javascript. What you want is

<script type="text/javascript"> 
if("whatever is in the $re vairable"){
    document.getElementById('hide').style.display = "none"; } 
</script>

Note the " which ensures that the whole thing is valid Javascript and that the contents of $re will be interpreted as an Javascript string by the Browser's Javascript engine. The call to json_encode() does exactly this - it transforms PHP variables into the appropriate Javascript variables.

Saturday, May 29, 2021
 
linjuming
answered 5 Months ago
32

Because morning and night are tinyint you should pass numbers(integers) 0=false and 1=true.

Also your mysqli_ code is a little odd, when using prepared parameterised queries you do not concatenate values into the query text, you use ? parameters

$id = "104"
$bool1 = 1
$bool2 = 0

$sql = "INSERT INTO Monday  SET Id = ? , morning = ? , night = ?" ;

$result = $conn->prepare($sql) ;

// Get the result  in a Returnvalue var
if (!$result) {
    throw new Exception($result->error);
}
$result->bind_param("sii" ,$patientId , $morning , $night );
$returnValue = $result ->execute();
return $returnValue;

You might also like to rethink your database design. Having a table for each day is very odd. Instead consider a table with a more generic name, and in it have a column called dayname or something like that.

So your table might look like this

id
dayname
morning
night

and a single table would hold all the days of the week

Saturday, May 29, 2021
 
steros
answered 5 Months ago
38

As the PHP manual says:

empty — Determine whether a variable is empty

In your case, trim is a function call, not a variable.

If you really want to do your if statement inline, you can use something like:

if (!empty($var=trim($_POST['username'])) && !empty($var=trim($_POST['email'])))

But a better implementation should be:

$username = array_key_exists('username', $_POST) ? trim($_POST['username']) : null;
$email = array_key_exists('email', $_POST) ? trim($_POST['email']) : null;

if (!empty($username) && !empty($email))
{
    (...)
Tuesday, October 12, 2021
 
Bryan
answered 1 Week ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :