1

I am learning to code using AngularJS and php and have been stuck while trying to write the value of one of the variable to a text file using PHP.

My variable is,

{{sample.getString()}}

When I try to print it using php it works for me,

<?php echo "{{sample.getString()}}" ?>

however, when I try to write it in a text file,

<?php 
    $myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
    $txt = "{{sample.getString()}}";
    echo $txt;
    fwrite($myfile, $txt);
    fclose($myfile);
    ?>

It writes the variable itself instead of writing the value of the variable.

Can someone please tell me how can i fix this ?

  • You are trying to display an angular variable with PHP. The PHP code runs in your server, so the code that saves the variable to the file does not have the value you are expecting, it is a simple string "{{sample.getString()}}", when you are printing it with `echo` then this string goes to the client side, and it is executed by the browser or any script/library that can understand the syntax. Take a look at this SO answer to understand better the difference the server side and client side scripting https://stackoverflow.com/a/17403881/5064866 – knetsi Jan 18 '18 at 03:58

0 Answers0