Zweistein2 Zweistein2 - 1 year ago 46
PHP Question

if statement unusual behavior


I have a little website and want to create a pdf out of it. For this I use dompdf and some basic php:

* @Route ("/generatePDF/{php}", name="generatePDF")
public function generatePDFAction($php)
$isPHP = $php;

if($isPHP == true)
$lang = "php";
$lang = "js";

//open a link with $lang and get it's html (=> $html)

//Just for testing, normally it's a working websites html-code
$html = $isPHP;

$dompdf = new Dompdf();
$dompdf->setPaper('a4', 'landscape');
$pdf = $dompdf->output();

//$isPHP = false;

if($isPHP == true)
file_put_contents("downloads/rulesphp.pdf", $pdf);
return new Response($this -> redirect('/downloads/rulesphp.pdf'));
file_put_contents("downloads/rulesjs.pdf", $pdf);
return new Response($this -> redirect('/downloads/rulesjs.pdf'));

then call it:

<a class="btn btn-default" href="{{ path('generatePDF', {'php': 'true'}) }}">PDF aktualisieren</a>


<a class="btn btn-default" href="{{ path('generatePDF', {'php': 'false'}) }}">PDF aktualisieren</a>

whether I want the php or the js rules.

is a boolean I handover to the function. The first if-statement works like charm, it only breaks at the end...
$html is normally the websites html, I just set it to
for testing purpose (to see if
). Now the problem:

Whenever I run my code it creates and redirects me to a rulesphp.pdf, whether or not
is set to "true" or "false".

Sidenote: When I uncomment the "
//$isPHP = false
" - line it creates a "rulesjs.pdf", as expected, but if I comment it in again or delete it, it won't go back to it's old behavior (as if
is always
-> creating rulesphp.pdf, regardless of what
is set), it just keeps the "creating rulesjs.pdf /
is always

is set correctly, so if it's true, then the pdfs content is true (because of
$html = $isPHP
), if it's false the pdfs content is false, too.

Answer Source

@Route ("/generatePDF/{php}", name="generatePDF")

Your routing pass you string parameters. So, if you call it by /generatePDF/true, /generatePDF/false urls - you receive strings.


$isPHP === 'true'

or $isPHP = $php === 'true'