Mysidia Adoptables Support Forum  

Home Community Mys-Script Creative Off-Topic
Go Back   Mysidia Adoptables Support Forum > Mysidia Adoptables > Tutorials and Tips

Notices

Reply
 
Thread Tools Display Modes
  #1  
Old 02-22-2011, 07:14 PM
Hall of Famer's Avatar
Hall of Famer Hall of Famer is offline
Administrator, Lead Coder
 
Join Date: Dec 2008
Location: South Brunswick
Posts: 4,448
Gender: Male
Credits: 481,182
Hall of Famer is on a distinguished road
Default Hall of Famer's PHP Tutorials: Chapter 1 - PHP syntax

Chapter I - PHP Syntax

Well this is the first part of my PHP tutorials, which is all about basic syntax of PHP. In this thread you will be able to learn basic PHP syntax, variables, data types and constants. Alright, lets get this started.


1. Getting started with PHP:
If you have basic knowledge, you should understand the concept of tags. PHP tags enclose lines of PHP codes, you cannot write your PHP codes outside of tags. The start and end tags for PHP is shown below:

PHP Code:
<?PHP
// your codes here
?>
Short tags and complete scripting language tags can be used to replace this basic PHP tags at times:

PHP Code:
<?
// your codes here
?>
PHP Code:
<script language "php">
// your codes here
</script> 
2. Basic Syntax:
To display a message on the screen, we can use the functions print or echo. Strictly speaking, these are not the type of functions we normally refer to, since there is no need to enclose anything inside parenthesis(). An example is shown below:

PHP Code:
print "hello world";
echo 
"What a beautiful day!"
You may wonder what the semicolon signs are at the end of each line, they represent the end of a PHP statement and must not be left out in PHP programming. Finishing your lines without semicolons will cause severe syntax error, something you apparently do not wish to ever run into.

At times it is rather necessary to write comments so that your codes make sense to you even if you have taken a long break from PHP programming. It will also help other programmers read your script files. To write comments, simply use double slashes in front of your sentences:

PHP Code:
// This is a comment 
Now that we have understood the basis of PHP syntax, lets move on to variables, something you will have to use a lot in any programming languages.


3. Variables:
A variable is a type of container, which holds one particular value that can be of any data type. You can only assign one value to a variable, and the value can be changed at any time. To create a PHP variable, use the syntax:

PHP Code:
$var value
Here the dollar sign "$" indicates that you are working with a variable, 'var' is the name of our variable. The 'value' is whatever you assign to this variable, it can be a strong, an integer, a double floating number and so on. The equal sign "=" is technically not an equal sign, it assigns a value to the variable you define. Examples of Variables definition are shown below:

PHP Code:
$num 10;
$name "Sarah";
$pi 3.14159265
Do not ever forget the semicolon at the end of each line, or you will receive a fatal error. An interesting fact about PHP is that it automatically recognizes data types such as int, string or double. This saves you significant amount of work when defining a variable.

Another very important thing about variables is that its name can only contain letters, numbers or underscores. A variable name beginning with a number is not valid, nor is a variable that contains spaces. Examples of valid variable names are shown below:

PHP Code:
$name
$w75t
$_t013yuf 
The following variables names are, however, invalid in PHP:

PHP Code:
$8543pod
$re
+arb
$my food 

4. Data Types:
Data types are not that important in PHP compared to C++ and Ruby, but it is still important to play around with them. There are six basic data types, namely:

integer: 8, 99, 100000
double: 1.5, 2.6666, 3.14
boolean: true and false
string: "Anna", "Hello World".
array
object
null
resource

It may be necessary to see what data type a variable is currently holding. To do this, use the function below:

PHP Code:
gettype($var
The data type of variable $var will be printed to the screen if 'echo' is added right in front of the function gettype(). Another useful function is to set datatype to a different one:

PHP Code:
settype($vardatatype
This function takes in two arguments, the variable name and the newly assigned datatype. This way you can convert a variable from integer to double or string easily. A good example is shown below:

PHP Code:
$num 3.1415926;
settype($numint);
echo 
$num
The result is integer 3 on your screen, isnt this amazing? Datatype conversion is usually done by PHP itself, but there are times that you will have to do this yourself.

You may also use PHP's built-in functions is_datatype($var) to test if a variable contains the desired datatype. This types of functions return true if the variable contains the same datatype, or it returns false. They can be easily integrated with if...else statements.

A list of this type of functions is shown below:
PHP Code:
is_int($var)
is_double($var)
is_string($var)
is_bool($var)
is_array($var
is_object
($var)
is_null($var)
is_resource($var
5. Constants:
Constants, in contrast to variables, are containers storing values that cannot be changed later in your codes. To create a PHP constant, use the following syntax below:

PHP Code:
define("Constant's name"value); 
Keep in mind that once a constant has been defined, its value will no longer subject to reassignment. An example of constant definition is shown below:

PHP Code:
define("NAME""Richard");
echo 
NAME
The screen shall display the name Richard. Note it is common practice to define your constants with all upper-case letters, or the script may confuse a constant with a string.


Alright, this is all for basic syntax. The next tutorial will introduce the concept of operators and how they work in PHP. This is my first time writing tutorials for PHP programming, lemme know if you have any suggestions on this. Thanks for reading this thread everyone, and have fun with Mysidia Adoptables script files. ^^
__________________


Mysidia Adoptables, a free and ever-improving script for aspiring adoptables/pets site.
Reply With Quote
  #2  
Old 02-23-2011, 06:09 AM
Knyfe's Avatar
Knyfe Knyfe is offline
Things are looking harder
 
Join Date: Jan 2011
Location: MA, United States (That's one reason why I love this site XD)
Posts: 48
Gender: Female
Credits: 6,942
Knyfe is on a distinguished road
Default

This helps a lot! Thanks! Unfortunately I'll have to read it a few more times because my brain is slow on coding. :3
__________________
Just because we all use the same script doesn't mean our websites can't be unique.
Please visit Shop 12.
Reply With Quote
  #3  
Old 02-25-2011, 06:47 PM
Hall of Famer's Avatar
Hall of Famer Hall of Famer is offline
Administrator, Lead Coder
 
Join Date: Dec 2008
Location: South Brunswick
Posts: 4,448
Gender: Male
Credits: 481,182
Hall of Famer is on a distinguished road
Default

XD Its alright Knyfe, dont worry about it if you do not understand what I am saying in the first place. Its always the hardest to begin with, you will get used to programming with time.
__________________


Mysidia Adoptables, a free and ever-improving script for aspiring adoptables/pets site.
Reply With Quote
  #4  
Old 02-28-2011, 02:24 AM
RoconzaArt's Avatar
RoconzaArt RoconzaArt is offline
Member
 
Join Date: Jan 2011
Location: NJ Shore (and proud of it)
Posts: 479
Gender: Female
Credits: 56,900
RoconzaArt is an unknown quantity at this point
Default

Thank you for posting these.
__________________
Reply With Quote
  #5  
Old 02-28-2011, 02:27 AM
Hall of Famer's Avatar
Hall of Famer Hall of Famer is offline
Administrator, Lead Coder
 
Join Date: Dec 2008
Location: South Brunswick
Posts: 4,448
Gender: Male
Credits: 481,182
Hall of Famer is on a distinguished road
Default

You are very welcome. Cant say how much it helps, but I've decided to post more about PHP anyway. A support forum of a PHP based script definitely needs programming tutorials like this.
__________________


Mysidia Adoptables, a free and ever-improving script for aspiring adoptables/pets site.
Reply With Quote
  #6  
Old 02-28-2011, 10:31 AM
shiann's Avatar
shiann shiann is offline
Horse Tamer :3
 
Join Date: Jun 2010
Posts: 70
Gender: Female
Credits: 6,903
shiann
Default

So what is the difference between Print and Echo?.. Do they both just show whatever you put afterwords?
__________________
Hai there. :3 All I got for now.
Reply With Quote
  #7  
Old 02-28-2011, 12:04 PM
fadillzzz fadillzzz is offline
Dev Staff
 
Join Date: Jan 2010
Posts: 501
Gender: Male
Credits: 37,452
fadillzzz is an unknown quantity at this point
Default

Echo is slightly faster than print because it doesn't have a return value.
It's also 1 less character than print so it's really useful for the lazy people
Echo can take multiple expressions while print can't.
PHP Code:
echo "Hello" "World"
Other than that, there's nothing different about these two
Reply With Quote
  #8  
Old 02-28-2011, 03:57 PM
shiann's Avatar
shiann shiann is offline
Horse Tamer :3
 
Join Date: Jun 2010
Posts: 70
Gender: Female
Credits: 6,903
shiann
Default

Quote:
Originally Posted by fadillzzz View Post
Echo is slightly faster than print because it doesn't have a return value.
It's also 1 less character than print so it's really useful for the lazy people
Echo can take multiple expressions while print can't.
PHP Code:
echo "Hello" "World"
Other than that, there's nothing different about these two
What's a return value? other than that, I get it, thank you. :)
__________________
Hai there. :3 All I got for now.
Reply With Quote
  #9  
Old 03-01-2011, 01:17 AM
fadillzzz fadillzzz is offline
Dev Staff
 
Join Date: Jan 2010
Posts: 501
Gender: Male
Credits: 37,452
fadillzzz is an unknown quantity at this point
Default

Quote:
Originally Posted by shiann View Post
What's a return value? other than that, I get it, thank you. :)
http://en.wikipedia.org/wiki/Return_statement

As far as I know, print always returns 1.
Also I just found out that you can do the following with print but no echo
PHP Code:
<?php $isFoo and print 'foo'?>
Reply With Quote
  #10  
Old 03-01-2011, 10:14 AM
Hall of Famer's Avatar
Hall of Famer Hall of Famer is offline
Administrator, Lead Coder
 
Join Date: Dec 2008
Location: South Brunswick
Posts: 4,448
Gender: Male
Credits: 481,182
Hall of Famer is on a distinguished road
Default

Well a return value is quite simple to understand. Assume you have a math function f(x) = x^2 - 4x + 3, the x value is the input value, while the f(x) value is the return value. Print and echo are technically PHP functions, but of special type since you do not need to write a parenthesis to enclose anything inside these functions. Moreover, these functions' return values are themselves. This is pretty much like f(x) = x in math, in which f(x) has exactly the same value as x.
__________________


Mysidia Adoptables, a free and ever-improving script for aspiring adoptables/pets site.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Hall of Famer's PHP Tutorials: Chapter 6 - PHP Arrays Hall of Famer Tutorials and Tips 0 03-16-2013 07:14 PM
Hall of Famer's PHP Tutorials: Chapter 5 - PHP Function Basics Hall of Famer Tutorials and Tips 0 11-23-2012 01:05 AM
Hall of Famer's PHP Tutorials: Chapter 4 - PHP Iteration Hall of Famer Tutorials and Tips 2 02-26-2011 09:48 AM
Hall of Famer's PHP Tutorials: Chapter 3 - PHP Conditional Statements Hall of Famer Tutorials and Tips 0 02-23-2011 05:59 PM
Hall of Famer's PHP Tutorials: Chapter 2 - PHP Operators Hall of Famer Tutorials and Tips 0 02-22-2011 07:26 PM


All times are GMT -5. The time now is 05:42 AM.

Currently Active Users: 728 (0 members and 728 guests)
Threads: 4,081, Posts: 32,029, Members: 2,016
Welcome to our newest members, jolob.
BETA





What's New?

What's Hot?

What's Popular?


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
vBCommerce I v2.0.0 Gold ©2010, PixelFX Studios
vBCredits I v2.0.0 Gold ©2010, PixelFX Studios
Emoticons by darkmoon3636