You can not do something like that.
This is a very typical error in people who go from programming desktop applications to web programming (I do not know if this is your case).
What you have to be very clear about in web programming is that you have two different code execution environments, which execute code in completely separate environments (most of the times even physically: server computer and client computer with the browser) and at different times.
You have to always keep in mind the life cycle of a web petition:
The browser makes a request for a web page to the server
The server code (in your case PHP) that generates the code (HTML, javascript, css) that should be sent to the client is executed on the server. So, basically, the PHP code is code that generates code. But it does not execute this generated code, but it sends it to the browser so that it is he who executes it.
The server sends the generated code (HTML, Javascript, CSS) to the browser
The browser interprets and executes the received code and displays the result
This is the reason why from your PHP code, which is running on the server at time 2, you can not access the result of the Javascript code that runs on the browser at time 4.
In your case, the PHP code what you would do is create the following output to send to the browser:
$comparar = true;
$comparar_js = "<script type='text/javascript'>confirm('¿Borrar?')</script>";
echo($comparar_js); // Salida: "<script type='text/javascript'>confirm(...
if($comparar==$comparar_js){ // Si true=="<script type='text...."
echo("Hola"); // Esto no se puede dar nunca
}else{
echo("No entra en el if"); // Salida: "No entra en el if"
}
What is sent to the browser is therefore:
<script type='text/javascript'>confirm('¿Borrar?')</script>
No entra en el if
In other words, I will show a confirm
dialog with the text ¿Borrar?
with whose result nothing is done and shows in the browser yes or yes the message No entra en el if
.
I hope it helps you clarify some ideas.