📜 ⬆️ ⬇️

Useful PHP Programming Tips

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

Content





')
echo
print



php.net ,

,


print()



1
,

echo
. :<br>

($success) ? echo '!': echo '...';

<br>

print
. PHP
<br>

echo ($success) ? '!' : '...';.


, .




echo
- . , : «echo» — , , «print» — , .

echo
.


, , ,

print
? —

echo
!


(
echo
, -
print
)


PHP — (
'
) (
"
). .
( ) ( ). . — (
echo "Hello, $name!";
), (
echo "Hello\nworld!";
) (
echo "${config['hello']}, ${position}th world!";
).
, , . — . , (, , ZDE , , ).

?


echo
:
echo string $arg1 [, string $...] )
, . .
echo 'Hello,' , 'world!';
echo 'Hello,' . 'world!';
. , : . , Wouter Demuynck, (, ).

heredoc?


PHP «HEREDOC». :


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


«HEREDOC» .

, , , .



define('N',PHP_EOL); echo 'foo' . N;
echo "foo\n";<br>
echo 'foo' . "\n";
, .

?>


, . , , , ,
header()
.


functions.php:<br>
<php<br>
function foo()<br>
{<br>
...<br>
}<br>
function bar()<br>
{<br>
...<br>
}<br>



php.net Zend Framework <a href=".

require, include, readfile


.

readfile


readfile
output. , — .

include require


, PHP .
require
, «! !» .
include
warning, . — require .

..._once?



include 

require
«_once»:

include_once; require_once<
. , ? .

require_once
, .


PHP :

file_put_contents('filename','data');

backwards-compatibility, :

// 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;
		}
	}
}

Source: https://habr.com/ru/post/11380/


All Articles