# Assigning a variable breaks the server

**URL:** https://discourse.roots.io/t/assigning-a-variable-breaks-the-server/20861
**Category:** sage
**Tags:** sage9
**Created:** 2021-05-30T12:11:16Z
**Posts:** 17

## Post 1 by @matteo_maria_ambrogi — 2021-05-30T12:11:16Z

hello peeps,

something weird happens to my code.  
I need to declare a class conditionally, BUT, if I try doing it, this makes server crash (Bad Gateway error)

```
...
@php
    if( !isset($_GET['t']) ){
        $results = json_decode(Search::getPostBasic(), true);
    }
    else {
        $results = json_decode(Search::getPostAdvanced(), true);
    }
       $results ? $columns = 'card-columns' : $columns = '';
  @endphp
 
  <div class="row no-gutters masonry mb-5">
    <div class="col-lg-10 mx-auto mb-5 {{$columns}}">
...
```

not a rocket science but this crashes the page

 ![Schermata 2021-05-30 alle 14.10.48](https://discourse.roots.io/uploads/default/original/2X/c/ce5711e6f3f2698081b12ba0d2f4d84e7b822a82.jpeg)

any hint???

---

## Post 2 by @matteo_maria_ambrogi — 2021-05-30T12:36:06Z

EDIT: seems to be connected to the json\_decode action…

```
[error] 531#0: *4437 upstream prematurely closed FastCGI stdout while reading response header from upstream, client: 127.0.0.1, server: ~^(www\.)?(?<domain>.+?).localhost.dev$, request: "GET /?t=a&s=ss HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "associazione.localhost.dev"
```

---

## Post 3 by @strarsis — 2021-05-30T15:10:55Z

Edit: `json_decode` returns `NULL` for an empty input string (in recent PHP 7.4). Are you correctly handling `NULL` values, e.g. when iterating over it in a loop in the template?

Is this a staging/production server? It may be a good idea to show a proper server error page to the visitor instead just an empty HTTP 500 header response.

---

## Post 4 by @alwaysblank — 2021-05-30T15:55:16Z

> [@matteo_maria_ambrogi](#):
>
> `Search::getPostAdvanced()`

> [@matteo_maria_ambrogi](#):
>
> `Search::getPostBasic()`

What do these do?

> [@matteo_maria_ambrogi](#):
>
> `$results ? $columns = 'card-columns' : $columns = '';`

Probably unrelated but I don’t recommend using ternary operators this way. When you use a ternary, you should be using the result, i.e.

```
$columns = $results ? 'card-coumns' : '';
```

IMO it’s easier to read and understand.

---

## Post 5 by @matteo_maria_ambrogi — 2021-06-01T13:55:20Z

hei :slight_smile:  
@strarsis it’s localhost, running on nginx.  
I am just decoding a json!!  
if not call json\_Decode it works…

---

## Post 6 by @strarsis — 2021-06-01T13:56:43Z

Check the PHP error logs, the nginx error log only indicates that PHP had an error. The PHP error log should show the actual error.

---

## Post 7 by @matteo_maria_ambrogi — 2021-06-01T13:57:11Z

they perform two custom queries.

the funny part is that the result is a json from any of those, ok?  
if I call json\_decode, it crashes, on localhost, nginx…not in production with apache

---

## Post 8 by @matteo_maria_ambrogi — 2021-06-01T13:58:15Z

> [@matteo_maria_ambrogi](#):
>
> ```
> [error] 531#0: *4437 upstream prematurely closed FastCGI stdout while reading response header from upstream, client: 127.0.0.1, server: ~^(www\.)?(?<domain>.+?).localhost.dev$, 
> request: "GET /?t=a&s=my_search_string HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "associazione.localhost.dev"
> ```

@strarsis this is the error I get…

---

## Post 9 by @strarsis — 2021-06-01T14:05:07Z

But this is the `nginx` error. Isn’t there a PHP log, too?  
`upstream` is the PHP FastCGI daemon which response `nginx` passes through.  
In this case PHP encountered an error and didn’t give `nginx` the expected response.

The error can be anywhere here, from some unexpected JSON input string to non-existing variable in a template.

---

## Post 10 by @matteo_maria_ambrogi — 2021-06-01T14:06:25Z

uh ok, don’t have a clue where to look for the log I should inspect then :sweat_smile:

---

## Post 11 by @strarsis — 2021-06-01T14:07:06Z

What kind of system is this? Vagrant, Docker container, Trellis staging?

---

## Post 12 by @matteo_maria_ambrogi — 2021-06-01T15:07:48Z

no is a simple localhost managed with brew

---

## Post 13 by @strarsis — 2021-06-01T15:17:44Z

Are you using a setup similar or equal to this?:

> **[Configure a local WordPress development on macOS from scratch | pawelgrzybek.com](https://pawelgrzybek.com/configure-a-local-wordpress-development-on-macos-from-scratch/)**
>
> So you’re about to build a WordPress website. An Apache HTTP Server, PHP and MySQL database is all that you need. Let me guide you step by step.

In any way there is a PHP daemon on your development system that got its own log.  
You need to get the log from it to find the exact reason.  
This is also the only proper way doing development/debugging. Everything else would be just guessing.

---

## Post 14 by @matteo_maria_ambrogi — 2021-06-03T12:50:37Z

yes it’s something similar (on nginx), the only thing I am missing is where to look for `PHP FastCGI daemon` log :smiley:

---

## Post 15 by @strarsis — 2021-06-03T13:12:17Z

When you can find the httpd/apache logfiles under `/var/log/httpd` or similar, the php-fpm log files should be found under a similar directory, e.g. `/var/log/php-fpm` or maybe `/var/log/php` (without the `-fpm`).

Edit: Also try out the [`locate` command](https://wiki.ubuntuusers.de/locate/) for a quick-and-dirty system-wide search for these files.

---

## Post 16 by @matteo_maria_ambrogi — 2021-06-06T09:37:31Z

> [@strarsis](#):
>
> When you can find the httpd/apache logfiles under `/var/log/httpd` or similar, the php-fpm log files should be found under a similar directory, e.g. `/var/log/php-fpm` or maybe `/var/log/php` (without the `-fpm` ).

how they are supposed to be called those log files?

ty

---

## Post 17 by @strarsis — 2021-06-06T14:11:31Z

When you got console access and can run the php CLI, you should be able to let it print the exact path  
(`php --info | grep error`):

> <https://stackoverflow.com/questions/5127838/where-does-php-store-the-error-log-php5-apache-fastcgi-cpanel>

When systemd/journald is used, you have to query the log differently  
(`journalctl -u nginx -u php-fpm --since today`):

> **[Using journalctl as the ultimate utility for troubleshooting LEMP stacks -...](https://www.getpagespeed.com/server-setup/using-journalctl-as-the-ultimate-utility-for-troubleshooting-lemp-stacks)**
>
> Using journalctl for troubleshooting and logging data from your LEMP stack. The necessary configuration for logging PHP scripts errors

---

## Post 18 by @system — 2021-07-11T12:14:16Z

This topic was automatically closed after 42 days. New replies are no longer allowed.
