Jump to content

Recommended Posts

hey, i finally decided to give php a chance... ive been in actionscript all my life... i know some html and java but anyways...

...

can anyone here give me examples of basic code that is good to know and teach me something about php so i can start learning or give me some links to good beginers tuts. thankyou! ;D

 

 

in otherwords... im a noob who knows nuthing about php and need help, tell me some basics, thanks :P

Link to comment
https://forums.phpfreaks.com/topic/70701-im-a-noob-help-me-with-basics/
Share on other sites

<?php

- Opening tag for PHP (long-hand).

<?

- Opening tag for PHP (short-hand).

?>

- Closing tag for PHP.

 

echo "Hello world!";

- This will output "Hello world!". The echo statement can be used to display various arrays, functions etc. HTML can also be embedded in an echo statement, replace any double-quotes that HTML would use with single-quotes.

print "Hello world!";

- The print statement is almost identical to the echo statement.

 

$foo = "Billy";
echo "My name is $foo.";

- This echo statement will output "My name is Billy.". It makes use of a variable.

 

$array['0'] = "Billy";
$array['1'] = "Sally";
echo "My name is $array['0'], my sister's name is $array['1'].";

- This echo statement will output "My name is Billy, my sister's name is Sally.". It makes use of variable arrays ($array[key]).

 

If you are still interested in learning some more PHP, feel free to wander over to http://www.tizag.com to pick up some more basic knowledge. If you would like to learn how to use more advanced PHP, wander over to http://www.php.net and take a look at the manual. Good luck! :)

Commenting

<?php

//this is a comment
# this is also a comment
/*
this
is
also
a
comment
*/

?>

 

Requesting

<?php
$variable_get = $_GET['variable']; //requesting url configuration: index.php?variable=value
$variable_post = $_POST['variable']; //requesting form configuration, sent by form method post
$variable_req = $_REQUEST['variable']; //requesting from both get/post, will find one, and use that one
$variable_ses = $_SESSION['variable']; //session variable, define by $_SESSION['variable'] = "value";
$variable_coo = $_COOKIE['variable']; //cookie variable, define by using setcookie();
$variable_ser = $_SERVER['REMOTE_ADDR']; //will return user's IP using the server request method
?>

 

Arrays

<?php
$array = array(); //array with no values

$array[] = "value1"; //will set the 0 key to the $array variable

$array2 = array('value1','value2','value3'); //array with 3 true values
?>

 

Echo/Print

Difference between the two:

Even though print() and echo() are essentially interchangeable most of the time, there is a

substantial difference between them. While print() behaves like a function with its own

return value (although it is a language construct), echo() is actually a language construct that

has no return value and cannot, therefore, be used in an expression.

 

Using Echo/Print

<?php
echo constant; //will echo constant unless constant is defined
echo "string"; //will echo string with usage of double quotes
echo 'string'; //also a string using single quotes
echo $variable; //will echo the value of the variable $variable

//print
print constant; //will print constant unless constant is defined
print "string"; //same
print 'string'; //same
print $variable; //same
?>

ok i wanted to knwo is there an easier way to make two echo lines break so instead of saying...

 

my name is billy. you name isnt billy

 

 

it will say...

 

my name is billy.

you name isnt billy.

 

 

 

this is the code ive got so far

 

<?php
$foo = "billy";
echo "My name is $foo.";
echo "<br>";
echo "you name isnt $foo";
?>

<?php
$string = "i|like|using|explode|to|show|every|single|partical|of|text|in|this|string";

$boom = explode("|",$string);

foreach($boom AS $explosions){
echo $explosions . "<br />\n";
}
?>

 

exploding shit is fun :)

 

Hell yeah it is ;D

 

ok i wanted to knwo is there an easier way to make two echo lines break so instead of saying...

 

my name is billy. you name isnt billy

 

 

it will say...

 

my name is billy.

you name isnt billy.

 

 

 

this is the code ive got so far

 

<?php
$foo = "billy";
echo "My name is $foo.";
echo "<br>";
echo "you name isnt $foo";
?>

 

<?php
$foo = "billy";
echo "My name is $foo.<br>you name isnt $foo";
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.