Last Updated: September 09, 2019
·
1.538K
· jammer9

Some cool things from the world of PHP

1 Built-in web server

Most PHP developers don't know that since PHP 5.4 the language comes with an integrated development server, which means that you don't need to set up an Apache Virtual Host for developing your project.

In the command line set the path to the document root or where the code lies.

$ cd ~/public_html
$ php -S localhost:8000

Read More : http://php.net/manual/en/features.commandline.webserver.php

2 Remove duplicate values from the array

$a = array_flip(array_flip($a));


<?php
$a = array('1','1','2','2','3','3');
printr(arrayflip(array_flip($a)));
/*
Result:
Array ( [1] => 1 [2] => 3 [3] => 5 ) 
*/
?>
</pre></code>

3 Variable variable names

A variable variable takes the value of a variable and treats that as the name of a variable.

<?php $a="php"; $$a="cool"; echo "$a is $php"; ?>
/*
Output : php is cool
*/
</pre></code>

Read More : http://php.net/manual/en/language.variables.variable.php

4 Swapping two variables

list($a, $b) = array($b, $a);

5 Responses
Add your response

Tip 2 can be replaced with http://be2.php.net/array_unique

over 1 year ago ·

A prefer to assign values within a string with sprintf, which allow me to replace the pattern easly :)

<?php $var = 'hello, ' . $world . '!';?>

vs.

<?php $var = sprintf('hello, %s!', $world);?>
over 1 year ago ·

@tschuermans thanks for sharing.

over 1 year ago ·

2 Remove duplicate values from the array

ehm...

$array = array_unique($array);

3 Variable variable names

Seriously: Don't use it. Its a good way to introduce hard to find bugs and to annoy your coworkers.

over 1 year ago ·

2 is wrong. 3 is just terrible practice. This isn't really anything cool.

over 1 year ago ·