Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

[PHP] Starters tutorial for function functionHere()

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
SNN
Posts: 30
Joined: 2006-12-07 12:39

[PHP] Starters tutorial for function functionHere()

#1 Post by SNN »

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. -

User avatar
Optional
Posts: 326
Joined: 2007-02-05 05:02

#2 Post by Optional »

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
_FOCUS_
Posts: 205
Joined: 2006-02-22 18:11

#3 Post by _FOCUS_ »

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.

Post Reply