Skip to content

02_Basic_usage

heguangyu5 edited this page Jun 30, 2022 · 1 revision

1. compile a single php file

$ mkdir /tmp/bpc
$ cd /tmp/bpc
$ cat -n a.php 
     1	<?php
     2	
     3	echo "hello world\n";

$ bpc a.php
$ ./a 
hello world

2. compile multiple php files

$ cat -n main.php 
     1	<?php
     2	
     3	if ($argc == 1) {
     4	    include "inc1.php";
     5	} else {
     6	    include "inc2.php";
     7	}
     8	
     9	sayhi();
    10	
    11	$f = "sayhi";
    12	$f();

$ cat -n inc1.php 
     1	<?php
     2	
     3	echo "This is inc1.php\n";
     4	
     5	function sayhi()
     6	{
     7	    echo "I am inc1.php\n";
     8	}

$ cat -n inc2.php 
     1	<?php
     2	
     3	echo "This is inc2.php\n";
     4	
     5	function sayhi()
     6	{
     7	    echo "I am inc2.php\n";
     8	}

$ bpc main.php inc1.php inc2.php
$ ./main 
This is inc1.php
I am inc1.php
I am inc1.php
$ ./main arg1
This is inc2.php
I am inc2.php
I am inc2.php

3. change default output executable name

$ bpc -o sayhi main.php inc1.php inc2.php
$ ./sayhi 
This is inc1.php
I am inc1.php
I am inc1.php
$ ./sayhi arg1
This is inc2.php
I am inc2.php
I am inc2.php

4. BPC_AUTO_RUN=TRUE

by set BPC_AUTO_RUN env to TRUE, compile and run can be done in one step.

$ BPC_AUTO_RUN=TRUE bpc a.php
hello world

$ BPC_AUTO_RUN=TRUE bpc main.php inc1.php inc2.php
This is inc1.php
I am inc1.php
I am inc1.php

$ BPC_AUTO_RUN=TRUE bpc main.php inc1.php inc2.php -- script-arg1
This is inc2.php
I am inc2.php
I am inc2.php

5. display_errors=on

you can set php ini by -d option.

$ cat -n err.php 
     1	<?php
     2	
     3	var_dump(mb_substr("abc", new stdclass));
     4	
     5	f();

$ BPC_AUTO_RUN=TRUE bpc -d display_errors=on err.php

Warning: mb_substr() expects parameter 2 to be integer, object given in bpc/err.php on line 3
NULL

Fatal error: Uncaught Error: Call to undefined function f() in bpc/err.php:5
Stack trace:
#0 {main}
  thrown in bpc/err.php on line 5

Clone this wiki locally