• Hello Fabrik Community

    Fabrik is now in the hands of the development team that brought you Fabrik for Joomla 4. We have recently transitioned the Fabrik site over to a new server and are busy trying to clean it up. We have upgraded the site to Joomla 4 and are running the latest version of Fabrik 4. We have also upgraded the Xenforo forum software to the latest version. Many of the widgets you might have been used to on the forum are no longer operational, many abandoned by the developers. We hope to bring back some of the important ones as we have time.

    Exciting times to be sure.

    The Fabrik 4.0 Official release is now available. In addition, the Fabrik codebase is now available in a public repository. See the notices about these in the announcements section

    We wish to shout out a very big Thank You to all of you who have made donations. They have really helped. But we can always use more...wink..wink..

    Also a big Thank You to those of you who have been assisting others in the forum. This takes a very big burden off of us as we work on bugs, the website and the future of Fabrik.

Using eval

  • Views Views: 26,747
  • Last updated Last updated:
  • 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.

    Using Eval​

    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:
        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.
Back
Top