1

my PHP :

<?php

function myFunc($echo) {
    //this js will print display the value of "myvar"
    $myvar = "<script type='text/javascript'>
                  var myvar= true;
                  document.write(myvar);
              </script>";
    //$myvar = "aDuck";
    if ($echo) {
        echo $myvar;
    }
}
function foo($echo) {
    if ($echo) {
        //this echoes "true", which is the value of js "myvar"
        echo "true";
    }
}
if (myFunc(true) == foo(true)) {
    echo "<br> WOW! it works!";
}
?>

I want PHP to test the value of a javascript variable in the same file without refreshing the page. I know there are ways to pass js vars to php through multiple files (ajax, json...) but I was curious and wanted to know if this could possibly work. It does, but I am not satisfied because PHP actually executes the functions.

Here's the output :

truetrue

WOW! It works!

The actual condition that is tested seems to be "this function echoes true".

So now for the questions :

  1. How do I prevent this from echoing true ?
  2. Is there a way to make PHP test myFunc and foo without
    actually executing those functions ?
  3. As the javascript is loaded with the page, is there a way for the user to modify myvar before it is read by the PHP script ?
AniMir
  • 150
  • 12
  • Use return instead of echo – d3t0x Apr 06 '19 at 15:16
  • you do not compare the _functions_. you are comparing what the functions _return_. which is, in both cases, `null`. you can not stop the functions from echoig true, because that's what they do. there is no way. and no, you can not. – Franz Gleichmann Apr 06 '19 at 15:17
  • @FranzGleichmann You are absolutely right. This is a total nonsense. Thanks for the answer! So is there any way for me to test if the output of is the same as ? – AniMir Apr 06 '19 at 15:30
  • 1
    https://stackoverflow.com/questions/171318/how-do-i-capture-php-output-into-a-variable may be useful. – Nigel Ren Apr 06 '19 at 15:34
  • Oh yes it defenitely is ! Thank you @NigelRen ! – AniMir Apr 06 '19 at 15:46
  • PHP runs on the server. It generates an HTML page (with JavaScript, CSS, etc.) for the web browser to run. It sends that to the browser, then stops running. The browser then gets the HTML to display and the JavaScript to run. By that point, like I said, the PHP code on the server has completed. Also, I don't know of browsers support `document.write` anymore... – gen_Eric Dec 19 '19 at 16:25

3 Answers3

0

PHP runs on the server, to generate a webpage for the web browser (the "client") to run. The client gets this output and processes it. In the case of a web browser, it displays the HTML and executes the JavaScript.

In your code, $myvar contains a string starting with <script, it does not contain true. PHP cannot run JavaScript code, that's the browser's job.

Also, when you do echo, what that does is add that to the output it's sending to the browser. So, what your code is doing is, sending the browser some JavaScript code to run, sending the browser the word true (to be printed on the screen as if it were an HTML page) and then comparing the return values of 2 functions.

Those 2 functions do not return anything, or rather they return NULL, and you are comparing them. NULL == NULL is true so your <br> WOW! it works! is also printed to the screen (sent to the browser to be displayed as HTML).

There is no way for you to do what you are trying to do. You cannot compare the output of a JavaScript program with a PHP variable. You will need to think of another solution.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
0

Somebody posted a different question with a solution involving buffer manipulation, here: How do I capture PHP output into a variable?

I just wanted to say that if they're custom functions, a bit of minor refactoring will probably serve you better, in the long run. Instead of:

function foo() {
    echo 'blah';
}

Change it to this:

function foo() {
    echo bar();
}

function bar() {
    return 'blah';
}

This way, your existing calls to foo() remain unchanged, but if you just want the value you can call bar().

More specific to your question:

<?php
function myFunc( ...$params ) {
    $x = myFunc2( ...$params );
    if( $x !== false ) echo $x;
}
function foo( ...$params ) {
    $x = foo2( ...$params );
    if( $x !== false ) echo $x;
}

function myFunc2($echo) {
    //this js will print display the value of "myvar"
    $myvar = "<script type='text/javascript'>
                  var myvar= true;
                  document.write(myvar);
              </script>";
    //$myvar = "aDuck";
    if ($echo) {
        return $myvar;
    } else {
        return false;
    }
}
function foo2($echo) {
    if ($echo) {
        //this echoes "true", which is the value of js "myvar"
        return "true";
    } else {
        return false;
    }
}
if (myFunc2(true) == foo2(true)) {
    echo "<br> WOW! it works!";
}
?>

HOWEVER... if you're passing True to a parameter specifically intended to specify if you want to echo or not, but you don't want to echo, that suggests a more overarching structural problem in your code.

Stephen R
  • 3,512
  • 1
  • 28
  • 45
-2

You should pull "echo" logic outside functions like this.

function myFunc() {
  //this js will print display the value of "myvar"
  $myvar = "<script type='text/javascript'>
              var myvar= true;
              document.write(myvar);
            </script>";
  return $myvar;
}

// Here you use echo or not

if($condition){
  echo myFunc();
}
Alexander van Oostenrijk
  • 4,644
  • 3
  • 23
  • 37
Jakob
  • 115
  • 2