Skip to content

emacs-php/php-runtime.el

Repository files navigation

php-runtime.el

This package bridges to PHP: Hypertext Preprocessor.

Requirements

Example

;; Exapmle simple code
(php-runtime-eval "echo strtoupper('apple');")
;;=> "APPLE"

;; Shorthand syntax for a PHP expression
(php-runtime-expr "strtoupper('apple')")
;;=> "APPLE"

;; Execute specific PHP executable
(php-runtime-expr "PHP_VERSION") ; no specific
;;=> "7.1.8"

(let ((php-runtime-php-executable "/usr/bin/php"))
  (php-runtime-expr "PHP_VERSION"))
;;=> "5.6.30"

;; Get numeric value by PHP
(setq php-int-max (string-to-number (php-runtime-expr "PHP_INT_MAX")))

;; Evaluate PHP code with STDIN as a string
(princ (php-runtime-eval "while ($line = trim(fgets(STDIN))) { var_dump($line); }"
                         "apple\nbanana\norange"))

;; Evaluate PHP code with STDIN as a file
(princ (php-runtime-eval "while ($line = trim(fgets(STDIN))) { var_dump($line); }"
                         (cons :file "/etc/hosts")))

Construct PHP expression

(php-runtime-\' "You're wellcome.")
;;=> "'You\\'re wellcome.'"

(let ((a "You'er")
      (b "wellcome"))
  (php-runtime-expr
   (format "implode([%s, %s], ' ')"
           (php-runtime-\' a)
           (php-runtime-\' b))))
;;=> "'You\\'re wellcome.'"