Using eval

May 12, 2015
Using eval
  • Eval is a method of running PHP code that you have entered in a Fabrik setting box. Since eval code can be any PHP code, it can be a very flexible way of achieving your Fabrik goals.

    Contents


    Using Eval (top)

    Throughout Fabrik there are often administration settings to create templates for sending emails, or for producing a default value. Often the text boxes which allow you to enter the template text are accompanied with an 'eval' checkbox. The Calc element is also designed to run eval on php code to calculate the value of a field from other data.

    As an example, to get a template whose output is 'foo bar' your template might look like this
    PHP:
    $var = 'bar';
    return 'foo ' . $var;
    When you use these administrative settings, Fabrik runs whatever text you have typed in through PHP's eval function. The Eval function is quite strict on what you can give it to run, so you need to follow some ground rules in order to be successful in your use of it:
    • Unlike PHP files which start as HTML files and only runs PHP code between <?php and ?>, the eval function assumes that all the text is PHP - so you can't start / end or use <?php and ?> in your code;
    • The results of the eval are used by Fabrik, so you need ALWAYS to return a string using the PHP return statement;
    • Eval checks the syntax of the PHP code before running it, so it needs always to be valid PHP even if e.g. a placeholder is empty or unexpectedly holds alphanumeric text rather than the number you were expecting to be in it. Therefore:
    • If you use Fabrik placeholders inside your eval'd PHP code, these should always be encased in quotes e.g.
      • If you use the following code:
        PHP:
        $name = '{contacts___name}';
        return 'my name is ' . $name;
      • Then the placeholders are first replaced with values so the text to be eval'd becomes:
        PHP:
        $name = 'Rob';
        return 'my name is ' . $name;
      • Which will output:
        Code (Text):
        my name is Rob
    • If you need a numeric value from a placeholder to use in calculations, please use the php 'cast' functionality like this:
      PHP:
      $int = (int) '{contacts__age}';
      $age = $int + 1;
      return "your age next year will be" . $age;
    • If your code has a syntax error, the PHP eval function returns false and so we check this and show an error if the eval result is false. We recommend that you code your eval'd PHP in such a way that you avoid returning a false value from your own code in order to avoid confusion.
    • Eval'ed code in PHP runs in the same "scope" as the code that runs it, which means that it is easy to accidentally change existing variables. You should be careful to use some standard prefix for any variables you create, like $my_, to avoid overwriting Fabrik variables. This is especially true for common "Fabriky" things like $element, $formModel, $user, etc. So always use names like $my_element, or $myUser, to avoid this.
    Please view the common PHP tasks page for more information on using PHP within Joomla.
daydah likes this.