[PHP] Starters tutorial for function functionHere()

Need help with C, C++, perl, python, etc?

[PHP] Starters tutorial for function functionHere()

Postby SNN » 2007-03-03 04:29

This will show how to use basic function codes for PHP.
Code: Select all
<?php
// Set what ever you want functionHere() to be. MUST include ().
function testHere($x, $y) {
// Your content here.
echo "Your username is " . $x . "  and your Nick Name is " . $y . ".";
}
?>
<?php
// Include the file. Ours will be 'test.php'
include('test.php');
// Use and set functions. Where the first area of ($x, $y) is your username and $y is what you like to be called.
testHere(SNN,SNN);
?>
Massive Web Hosting - Talk N' - G.A. -
SNN
 
Posts: 30
Joined: 2006-12-07 12:39

Postby Optional » 2007-03-03 05:29

Code: Select all
<?php
// Set what ever you want functionHere() to be. MUST include ().
function testHere($x, $y) {
// Your content here.
echo "Your username is $x and your Nick Name is $y.";
}
?>
<?php
// Include the file and stop if it can't be included. Ours will be 'test.php'
@require_once('test.php') or die('Error: failed to include test file.');
// Use and set functions. Where the first area of ($x, $y) is your username and $y is what you like to be called.
testHere('my username','my nickname');
?>


Fixed. :P

Important files should use require() or require_once(), that way the script fails if they cannot be included for some reason. Prepending the function call with a @ supresses the error output. A failed include may give away the path to your PHP script/web folder, which will assist someone trying to compromise your site/server.

$variables can be inserted into any double-quoted string. If you need the character $ to appear inside a double-quoted string, use \$. Otherwise, use single-quoted strings (e.g. 'example') as they're parsed faster by the PHP interpreter.

------------------

Another small thing that most people don't realize:

PHP (like Perl, which most of PHP's half-implemented features are stolen from) supports "here document" syntax. Do NOT do retarded stuff like this: :roll:

Code: Select all
<?php

$html = "<html>";
$html .= "<head>";

..........

$html .= "</html>";

echo $html;
?>


Instead, opt for this:

Code: Select all
<?php

$html =<<<EOF
<html>
<head>

.............

</html>
EOF;

echo $html;
?>
Note that you can also insert $variables into the block of code between the EOFs (also, EOF can be whatever you want, but keep in mind that the ending EOF cannot have ANY spaces before it. It must be the first thing on the line and be by itself.)

You can also use here-documents to output things rather than assign them to a variable:

Code: Select all
<?php

$time = time();

echo <<<EOF

The current Unix timestamp is: $time

EOF;

?>


Amazing. ;) And makes code so much more readable, especially when dealing with HTML.
User avatar
Optional
 
Posts: 349
Joined: 2007-02-05 05:02

Postby _FOCUS_ » 2007-03-04 06:53

well, How I can start apache server during this work.
If you love something, let it go. If it comes back to you, it's yours. If it's run over by a car, you don't want it.
User avatar
_FOCUS_
 
Posts: 205
Joined: 2006-02-22 18:11


Return to Programming

Who is online

Users browsing this forum: No registered users and 5 guests

fashionable