PHP Tutorial

Zenax

Active Member
Messages
1,377
Reaction score
4
Points
38
Well I thought in order to test my skills I would write a tutorial on PHP! It is only basic, so to help out others etc.

PHP:
<?php
echo ('Hello World');
?>

Result: Hello World is written to the page!

PHP:
<?php
$text = "Hello World!";

echo ($text);
?>

Result is still Hello World on the page, however the text is stored in the variable $text

PHP:
<?php

$text[1] = "Hello World!";
$text[2] = "Hello Monkey!";

echo ($text[1]);
echo ($text[2]);


?>


Result is Hello World! Hello Monkey, written to the page. The variable is the same however the instances of them are different. Note the [1] and [2] next to them.

Hope you find it simple and easy to understand.

Author: Zenax
 

Derek

Community Support Force
Community Support
Messages
12,882
Reaction score
186
Points
63
Thanks for the tut helped me ALOT
 

Brandon

Former Senior Account Rep
Community Support
Messages
19,181
Reaction score
28
Points
48
You have this:

PHP:
 <?php

$text[1] = "Hello World!";
$text[2] = "Hello Monkey!";

echo ($text[1]);
echo ($text[2]);


?>

This should also work, not tested.


PHP:
<?php

// $text is an array!
$text[1] = "Hello World!";
$text[2] = "Hello Monkey!";

foreach ($text as $echo) {

echo $echo;

}

?>

Functions used: foreach(), echo()
 
Last edited:

Cubeform

New Member
Messages
339
Reaction score
0
Points
0
Or, for the array, you could do this:

PHP:
<?php
  $text= array("Hello World!","Hello Monkey!");
  // Note that the first item in the array would be $text[0]
?>

Or this:

PHP:
<?php
  $text= array(1 => "Hello World!", 2 => "Hello Monkey!");
?>

Or this:
PHP:
<?php
  $text= array("world" => "Hello World!", "monkey" => "Hello Monkey!");
?>

All three achieve the same result. All are tested and all work.
 

Brandon

Former Senior Account Rep
Community Support
Messages
19,181
Reaction score
28
Points
48
Or if you want to be advanced you can do this.

PHP:
<?php

$words = "Hello World!,Hello Monkey!";

$text = explode(",",$words);

?>

Of course, untested

Functions used: explode()
 
Last edited:

Zenax

Active Member
Messages
1,377
Reaction score
4
Points
38
Thank you guys! This was meant to be simple and easy for beginners and now I have fount even more ways of achieving this!

im just picking things up as I go along, like i got the $text[1] thing from a login script t2t2 wrote for me!
 

Brandon

Former Senior Account Rep
Community Support
Messages
19,181
Reaction score
28
Points
48
Thank you guys! This was meant to be simple and easy for beginners and now I have fount even more ways of achieving this!

im just picking things up as I go along, like i got the $text[1] thing from a login script t2t2 wrote for me!

Indeed you did I bet.

You can also do: $text['brandonrules']

that works too, just edit the 'brandonrules' to other text.
 
Last edited:

t2t2t

New Member
Messages
690
Reaction score
0
Points
0
PHP:
<?
$text = 'Hello';
if ($text = 'Hello') print $text .' World!'; // Hello World!
if ($text) print "$text World!"; // Hello World!
print '$text World'; // $text World
// (in single-quote, variables don't get changed, with double-quotes, PHP will ALWAYS look for a variable,
// so its most efficient to use first print)
print 'I said Hello to you at '. date(r) .'!';
?>
 
Last edited:

Zenax

Active Member
Messages
1,377
Reaction score
4
Points
38
Thanks again guys!

I wrote this:
PHP:
<?php

class hello
{

	function world()
	{
	
		echo ('Hello World!');
		
	}
	
	function monkey()
	{
	
		echo('Hello Monkey!');
		
	}
	
}

$helloworld = new hello;
echo $helloworld -> world();

print '<br />';

echo $helloworld -> monkey();

?>

It works, and does exactly the same as what the others do XD
 
Last edited:

Brandon

Former Senior Account Rep
Community Support
Messages
19,181
Reaction score
28
Points
48
There was no need for the second: $helloworld = new hello;
 
Top