Search code examples
phpyamlpecl

Why is yaml_parse_file returning true?


For some reason when I run the .php script:

$s = yaml_parse_file("../config.yaml") || die("YAML file not found");
var_dump($s);

it returns:

bool(true)

What on earth is going on? This has happened out of nowhere it was working fine for a week and I can't seem to fix it. I have installed using pecl install yaml and added "extension=yaml.so" to php.ini?

I have used online yaml regex testers and they return that it is okay. The format is (obviously with content):

title: 
email: hello@
logo: images/logo.png
download-file: .dmg
recaptcha:
  pub:
  priv:
meta:
  keywords: mac, osx
  description:
  ico: images/icon.ico

Solution

  • You're assigning the result of the boolean operation to $s, since the || operator has a higher precedence than the assignment. So it's being evaluated as follows:

    $s = (yaml_parse_file("../config.yaml") || die("YAML file not found"));
    

    This returns true, since the initial expression returns a "truthy" value.

    If you wrap the assignment in brackets, it will work as you are expecting:

    ($s = yaml_parse_file("../config.yaml")) || die("YAML file not found");
    ...
    

    See https://eval.in/960405