Useful PHP Programming Trivia

    The PHP programming language is very, very free. Because of this, unfortunately, there are many ways to write the same thing and not know what is best. In this topic, I will describe a few little things useful for beginners and slightly advanced PHP programmers.

    Content






    echo
    or
    print


    Functionality


    php.net offers a small article about the difference between them, but it is not very informative. The

    functional difference between them is only that

    
    print()
    


    returns
    
    1
    
    , a
    
    echo
    
    returns nothing. This can affect if only in this code:

    
    ($success) ? echo 'Ура!': echo 'Увы...';
    



    
    print
    
    would work here. In any case, such a sentence in PHP is not correct and it would be better to use

    
    echo ($success) ? 'Ура!' : 'Увы...';.
    


    The difference does not count, so we will talk about them as about the same constructs.


    Speed


    
    echo
    
    returns nothing and therefore a little bit faster. The difference in parsing is ridiculous, but what’s more interesting: "echo" - four letters that are very convenient at hand, and "print" - five, and slightly worse arranged.
    In speed
    
    echo
    
    preferable.

    Conclusion


    We can talk a lot about the fact that the difference in speed is very scanty, but if there is no difference in functionality, then why use
    
    print
    
    ? Our choice -
    
    echo
    
    !

    Building text for output


    (here I will use
    echo
    but the same thing applies to
    print
    )

    Find out the relationship with quotes


    Two types of quotes are used in PHP to display text - simple (
    '
    ) and double (
    "
    ) The difference between them is very big.
    In simple quotes, the parser searches only for a simple quote (as an end character) and a backslash (for entering a simple quote). In doubles, the parser can do much more. For example - see variables (
    echo "Hello, $name!";
    ), characters (
    echo "Hello\nworld!";
    ) and even trickier variables (
    echo "${config['hello']}, ${position}th world!";
    )
    Obviously, simple quotes are faster, the parser almost does not need to think. But the most important thing is the readability of the code. Designs with double quotes are not only harder to look at, many editors with code highlighting cannot see them (of course, monsters like ZDE can do this without any problems, but after all, the person editing the code after you may not have it).

    A period or a comma?


    Command
    echo
    has the following syntax:
    echo string $arg1 [, string $...] )
    , i.e., you can write as
    echo 'Hello,' , 'world!';
    like that
    echo 'Hello,' . 'world!';
    . In the first case, we pass two parameters, in the second one: glued together by the gluing operator. Using the dot, we force the parser to grab everything into memory for a while and glue it, as Wouter Demuynck points out , so the commas turn out to be faster (of course, checked by tests).

    heredoc?


    PHP has a HEREDOC construct. It looks like this:

    
    echo <<
    Hello,
    world,
    I love you!
    HEREDOC;


    Here, instead of “HEREDOC,” anything can be.

    True, you need to know about this design only that it is ten times slower than its colleagues, not very readable and therefore it is better not to use it.

    End of line


    
    define('N',PHP_EOL); echo 'foo' . N;
    more readable than
    echo "foo\n";
    echo 'foo' . "\n";
    but this is purely my opinion and here I am not sure.

    Sense?>


    Few people know that you do not need to put this construct at the end of the file. Moreover, it is much better when he is not there, then there is no danger that a gap will slip through there and destroy further sent
    header()
    .
    
    functions.php:
    function foo()
    {
    ...
    }
    function bar()
    {
    ...
    }


    php.net and Zend Framework also think so .

    require, include, readfile


    Often these functions are used without understanding the difference between them.

    readfile


    readfile
    just throws the contents of the file into output. If you need to show a menu or banner, then this function is just right.

    include and require


    These two functions parse the contents of a file, like a PHP file.
    require
    without finding the file it says “Ahhh! Wait! ”And throws a fatal mistake.
    include
    it treats the error calmly and simply gives a warning without interrupting execution. Conclusion - require is needed much more often.

    ..._ once?


    
    include 
    
    and
    
    require
    
    can be replenished with the postfix "_once":
    
    include_once; require_once<
    
    . In this case, the parser will check, maybe he has already added these files? Convenient for connecting libraries in large projects. You can not be afraid to add
    
    require_once
    
    to every module where a library is needed.

    Write to file


    Writing to a file with the fifth version of PHP is very simple:
    
    file_put_contents('filename','data');
    

    To maintain backwards-compatibility, you can use this code:
    
    // File put contents
    if (!defined('FILE_APPEND')) define('FILE_APPEND',1);
    if (!function_exists('file_put_contents'))
    {
    	function file_put_contents($n, $d, $flag = false) {
    		$mode = ($flag == FILE_APPEND || strtoupper($flag) == 'FILE_APPEND') ? 'a' : 'w';
    		$f = @fopen($n, $mode);
    		if ($f === false) {
    			return 0;
    		} else {
    			if (is_array($d)) $d = implode($d);
    			$bytes_written = fwrite($f, $d);
    			fclose($f);
    			return $bytes_written;
    		}
    	}
    }
    

    Also popular now: