Assigning data

Allmost all types of PHP variables can be assigned to a JS\Context object, and used inside the JavaScript code. Scalar variables are trivial, as we had seen in the hello world example:

<?php
// create a new context
$context = new JS\Context;

// assign data to the javascript engine
$context->assign('language', 'dutch');
$context->assign('name', 'Emiel');

// execute a statement concatenating into a very well-known greeting
$result = $context->evaluate("var x = language == 'dutch' ? ('Hallo ' + name) : ('Hello ' + name);");

// result now contains the string "Hallo Emiel" (because the language was set to dutch)
var_dump($result);
?>

Assigning arrays

Arrays can be assigned to the script as well, and are turned into Javascript arrays. As you may know, in Javascript everything is an object: even arrays. The PHP-JS extension takes care of this conversion, and converts the PHP array into an traversable Javascript array-object with a 'length' property:


<?php
// create a new context
$context = new JS\Context;

// assign data to the javascript engine
$context->assign('numbers', array(1,6,3,77,23,0,12));

// store javascript code in the $script variable using heredoc syntax
$script = <<< 'EOD'

    // javascript function to sum values in an array, using the
    // "for (var x in array)" syntax
    function sum_array1(items) {

        // result value
        var total = 0;

        // iterate over the items
        for (var x in items) total += items[x];

        // done
        return total;
    }

    // alternative javascript to sum an array, using the Array.length
    // property
    function sum_array2(items) {

        // result value
        var total = 0;

        // iterate over the items
        for (var i = 0; i < items.length; ++i) total += items[i];

        // done
        return total;
    }

    // sum the numbers twice, using both arrays
    sum_array1(numbers) + sum_array2(numbers);

EOD;

// execute a statement concatenating into a very well-known greeting
$result = $context->evaluate($script);

// result now contains the integer 244
var_dump($result);
?>

Found a typo?

You can find this documentation page on GitHub.
Propose a change