Selamat Datang Di Blog Aldot :)

Blog ini berisikan seputar tentang kehidupan saya,pengalaman saya,pengetahuan saya dan beberapa artikel yang saya ketahui. Terimakasih telah mengunjungi blog saya.

Selamat Datang Di Blog Aldot :)

Blog ini berisikan seputar tentang kehidupan saya,pengalaman saya,pengetahuan saya dan beberapa artikel yang saya ketahui. Terimakasih telah mengunjungi blog saya.

Selamat Datang Di Blog Aldot :)

Blog ini berisikan seputar tentang kehidupan saya,pengalaman saya,pengetahuan saya dan beberapa artikel yang saya ketahui. Terimakasih telah mengunjungi blog saya.

Selamat Datang Di Blog Aldot :)

Blog ini berisikan seputar tentang kehidupan saya,pengalaman saya,pengetahuan saya dan beberapa artikel yang saya ketahui. Terimakasih telah mengunjungi blog saya.

Selamat Datang Di Blog Aldot :)

Blog ini berisikan seputar tentang kehidupan saya,pengalaman saya,pengetahuan saya dan beberapa artikel yang saya ketahui. Terimakasih telah mengunjungi blog saya.

Wednesday, October 26, 2011

PHP If...Else Statements

Conditional statements are used to perform different actions based on different conditions.

Conditional Statements

Very often when you write code, you want to perform different actions for different decisions.
You can use conditional statements in your code to do this.
In PHP we have the following conditional statements:
  • if statement - use this statement to execute some code only if a specified condition is true
  • if...else statement - use this statement to execute some code if a condition is true and another code if the condition is false
  • if...elseif....else statement - use this statement to select one of several blocks of code to be executed
  • switch statement - use this statement to select one of many blocks of code to be executed

The if Statement

Use the if statement to execute some code only if a specified condition is true.

Syntax

if (condition) code to be executed if condition is true;
The following example will output "Have a nice weekend!" if the current day is Friday:

<html>
<body>

<?php

$d=date("D");
if ($d=="Fri") echo "Have a nice weekend!";
?>

</body>

</html> 
 
Notice that there is no ..else.. in this syntax. The code is executed only if the specified condition is true.

The if...else Statement

Use the if....else statement to execute some code if a condition is true and another code if a condition is false.

Syntax

if (condition)
  code to be executed if condition is true;
else
  code to be executed if condition is false;

Example

The following example will output "Have a nice weekend!" if the current day is Friday, otherwise it will output "Have a nice day!":

<html>
<body>

<?php

$d=date("D");
if ($d=="Fri")
  echo "Have a nice weekend!";
else
  echo "Have a nice day!";
?>

</body>

</html> 
 
If more than one line should be executed if a condition is true/false, the lines should be enclosed within curly braces:

<html>
<body>

<?php

$d=date("D");
if ($d=="Fri")
  {
  echo "Hello!<br />";
  echo "Have a nice weekend!";
  echo "See you on Monday!";
  }
?>

</body>

</html>


The if...elseif....else Statement

Use the if....elseif...else statement to select one of several blocks of code to be executed.

Syntax

if (condition)
  code to be executed if condition is true;
elseif (condition)
  code to be executed if condition is true;
else
  code to be executed if condition is false;

Example

The following example will output "Have a nice weekend!" if the current day is Friday, and "Have a nice Sunday!" if the current day is Sunday. Otherwise it will output "Have a nice day!":

<html>
<body>

<?php

$d=date("D");
if ($d=="Fri")
  echo "Have a nice weekend!";
elseif ($d=="Sun")
  echo "Have a nice Sunday!";
else
  echo "Have a nice day!";
?>

</body>

</html> 
 
 

PHP Operators

This section lists the different operators used in PHP.

Operator Description Example Result
+ Addition x=2
x+2
4
- Subtraction x=2
5-x
3
* Multiplication x=4
x*5
20
/ Division 15/5
5/2
3
2.5
% Modulus (division remainder) 5%2
10%8
10%2
1
2
0
++ Increment x=5
x++
x=6
-- Decrement x=5
x--
x=4

Assignment Operators

Operator Example Is The Same As
= x=y x=y
+= x+=y x=x+y
-= x-=y x=x-y
*= x*=y x=x*y
/= x/=y x=x/y
.= x.=y x=x.y
%= x%=y x=x%y

Comparison Operators

Operator Description Example
== is equal to 5==8 returns false
!= is not equal 5!=8 returns true
<> is not equal 5<>8 returns true
> is greater than 5>8 returns false
< is less than 5<8 returns true
>= is greater than or equal to 5>=8 returns false
<= is less than or equal to 5<=8 returns true

Logical Operators
Operator Description Example
&& and x=6
y=3

(x < 10 && y > 1) returns true
|| or x=6
y=3

(x==5 || y==5) returns false
! not x=6
y=3
!(x==y) returns true

References from http://www.w3schools.com/php/

PHP String Variables

String Variables in PHP

String variables are used for values that contain characters.
In this chapter we are going to look at the most common functions and operators used to manipulate strings in PHP.
After we create a string we can manipulate it. A string can be used directly in a function or it can be stored in a variable.
Below, the PHP script assigns the text "Hello World" to a string variable called $txt:

<?php
$txt="Hello World";
echo $txt;
?> 
 
The output of the code above will be:
Hello World
Now, lets try to use some different functions and operators to manipulate the string.

The Concatenation Operator

There is only one string operator in PHP.
The concatenation operator (.)  is used to put two string values together.
To concatenate two string variables together, use the concatenation operator:

<?php
$txt1="Hello World!";
$txt2="What a nice day!";
echo $txt1 . " " . $txt2;
?> 
 
The output of the code above will be:
Hello World! What a nice day!
If we look at the code above you see that we used the concatenation operator two times. This is because we had to insert a third string (a space character), to separate the two strings.


The strlen() function

The strlen() function is used to return the length of a string.
Let's find the length of a string:

<?php
echo strlen("Hello world!");
?> 
 
The output of the code above will be:

12 
 
The length of a string is often used in loops or other functions, when it is important to know when the string ends. (i.e. in a loop, we would want to stop the loop after the last character in the string).

The strpos() function

The strpos() function is used to search for a character/text within a string.
If a match is found, this function will return the character position of the first match. If no match is found, it will return FALSE.
Let's see if we can find the string "world" in our string:

<?php
echo strpos("Hello world!","world");
?> 
 
The output of the code above will be:

 
The position of the string "world" in the example above is 6. The reason that it is 6 (and not 7), is that the first character position in the string is 0, and not 1.

References from http://www.w3schools.com/php/

PHP Variables

Variables in PHP

Variables are used for storing values, like text strings, numbers or arrays.
When a variable is declared, it can be used over and over again in your script.
All variables in PHP start with a $ sign symbol.
The correct way of declaring a variable in PHP:

$var_name = value; 
 
New PHP programmers often forget the $ sign at the beginning of the variable. In that case it will not work.
Let's try creating a variable containing a string, and a variable containing a number:

<?php
$txt="Hello World!";
$x=16;
?>


PHP is a Loosely Typed Language

In PHP, a variable does not need to be declared before adding a value to it.
In the example above, you see that you do not have to tell PHP which data type the variable is.
PHP automatically converts the variable to the correct data type, depending on its value.
In a strongly typed programming language, you have to declare (define) the type and name of the variable before using it.
In PHP, the variable is declared automatically when you use it.

Naming Rules for Variables

  • A variable name must start with a letter or an underscore "_"
  • A variable name can only contain alpha-numeric characters and underscores (a-z, A-Z, 0-9, and _ )
  • A variable name should not contain spaces. If a variable name is more than one word, it should be separated with an underscore ($my_string), or with capitalization ($myString)
References from http://www.w3schools.com/php/

PHP Syntax

Basic PHP Syntax

A PHP scripting block always starts with <?php and ends with ?>. A PHP scripting block can be placed anywhere in the document.
On servers with shorthand support enabled you can start a scripting block with <? and end with ?>.
For maximum compatibility, we recommend that you use the standard form (<?php) rather than the shorthand form.
<?php
?> 
A PHP file normally contains HTML tags, just like an HTML file, and some PHP scripting code.
Below, we have an example of a simple PHP script which sends the text "Hello World" to the browser:

<html>
<body>

<?php
echo "Hello World";
?>

</body>
</html> 


Each code line in PHP must end with a semicolon. The semicolon is a separator and is used to distinguish one set of instructions from another.
There are two basic statements to output text with PHP: echo and print. In the example above we have used the echo statement to output the text "Hello World".
Note: The file must have a .php extension. If the file has a .html extension, the PHP code will not be executed.

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

Comments in PHP

In PHP, we use // to make a single-line comment or /* and */ to make a large comment block.
<html>
<body>

<?php

//This is a comment

/*

This is
a comment
block
*/
?>

</body>

</html> 


References from http://www.w3schools.com/php/

Use parenthesis in expression 2

<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
<!--
function calcArea()
{
    inpRadius = 3;
    document.write(Math.PI * ((inpRadius)*(inpRadius)));
}

function calcPeri()
{
    inpRadius = 3;
    document.write(* Math.PI * (inpRadius));
}

function calcVol()
{
    inpRadius = 3;
    document.write(Math.PI * ((inpRadius)*(inpRadius)*(inpRadius)) (4/3));
}
//  -->
</script>
</head>
<body>
<h1>All about Circles and Spheres!</h1>
<P>Click <input type="button" value="HERE" onclick="calcArea();"> to calculate circle area!</p>
<br>
<P>Click <input type="button" value="HERE" onclick="calcPeri();"> to calculate circle perimeter!</p>
<br>
<P>Click <input type="button" value="HERE" onclick="calcVol();"> to calculate sphere volume!</p>
</body>
</html>
 

Use parenthesis in expression

<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
<!--
    var inpRadius;
    inpRadius = 3;
    document.write(Math.PI * ((inpRadius)*(inpRadius)));
//  -->
</script>
</head>
<body>

</body>
</html>
 

Hide scripts using comments

<html>
<head>
<title>Hide scripts using comments.</title>
<script language="javascript" type="text/javascript">

<!--

lines of JavaScript code here
...

//-->

</script>

</head>
<body>

Page content here...

</body>
</html>
 

Multiline comments

/* */ enables comments to span multiple lines.

Example

<html>
<head>
<title>A Simple Page</title>
<script language="javascript">
<!--
/*
Below two alert() methods are used to
fire up two message boxes - note how the
second one fires after the OK button on the
first has been clicked
*/
alert("An alert triggered by JavaScript!");
alert("A second message appears!");
//  -->
</script>
</head>
<body>

</body>
</html>
 

Single line comments

The // comment tag enables comments to be placed between the // and the end of the line.

Example :

<html>
<head>
<title>A Simple Page</title>
<script language="javascript">
<!--
// The first alert is below
alert("An alert triggered by JavaScript!");
// Here is the second alert
alert("A second message appears!");
//  -->
</script>
</head>
<body>

</body>
</html>


References from  http://www.java2s.com/Tutorial/JavaScript/

HTML - Horizontal Rule

Use <hr/> to create a horizontal line. THis tag is similar with line break.
<hr/>
Use
<hr/><hr/>
With
<hr/>
Caution
<hr/>

Display


Use


With

Caution


The horizontal line can be useful in different circumstances, like you can see
in the next example : a footnote.
<hr />
<p>1. "The Hobbit", JRR Tolkein.<br />

2. "The Fellowship of the Ring" JRR Tolkein.</p>

Display


1. "The Hobbit", JRR Tolkein.
2. "The Fellowship of the Ring" JRR Tolkein.

As you can see, everything that this tag does is to draw a horizontal line
separating different parts of the content or it just decorates the page. Use
it with skill and it will give unexpected results.

Line break

A line break is different from the other tags that we have seen before. The place we put it in the source cod of the document, will end the line and it will go down with a line , letting a space which is smaller the the one from the paragraph. In the paragraph is used , as you can see, in the next example.

<p>
Charles Brownstone <br />
Florida Street No 1 <br />

New York, USA <br />
</p>

Display


Charles Brownstone

Florida Street No 1

New York, USA


 
As well it can be used for pointing out a signature , for example at the end

of a letter.
<p>
Thank You,<br />

<br />
<br />
Charles Brownstone <br />
Vice-President
</p>

Display


Thank You,

Charles Brownstone

Vice-President


References from http://www.tutorialehtml.com

HTML - Titles 1:6 (Headings)

In HTML, a title is exactly what it looks : a real title or a subtitle.When you put a text in a tag <h1> , the text will be bolded, and the dimension of the letter will be the same with the number of the heading. The titles can have dimensions form 1 to 6 , where 1 is the smallest dimension and 6 the biggest dimension.
<body>
<h1>Headings</h1>
<h2>are</h2>
<h3>great</h3>

<h4>for</h4>
<h5>titles</h5>
<h6>and subtitles</h6>

</body>

Display

You can see that every title has a linebreak before and after. Every time you put a heading, the browser will give automatically a space in the line before it and after as well.

You can see that every title has a linebreak before and after. Every time you put a heading, the browser will give automatically a space in the line before it and after as well.

HTML - Practical example

Lets take now an easy example and practice it for a bit so that the information will be understood totally. It would be recommended for you to get used to these terms before we go on.
<h2 align="center">HTML - Titles 1:6 (Headings) </
<p> A title in HTML is exactly what it looks : a real title, or a ... </p>
<p> A title in HTML is exactly what it looks : a real title, or a ... </p>

Display

HTML - Titles 1:6 (Headings)

A title in HTML is exactly what it looks : a real title or a subtitle.When you

put a text in a tag <h1> , the text will be bolded, and the dimension of the letter

will be the same with the number of the heading. The titles can have dimensions


form 1 to 6 , where 1 is the smallest dimension and 6 the biggest dimension.



A title in HTML is exactly what it looks : a real title or a subtitle.When you

put a text in a tag <h1> , the text will be bolded, and the dimension of the letter

will be the same with the number of the heading. The titles can have dimensions

form 1 to 6 , where 1 is the smallest dimension and 6 the biggest dimension.

References from http://www.tutorialehtml.com

Paragraph's attribute

The paragraph is a base element in text editing.The tag for a paragraph is <p>.
He places a empty line above and under the text to make it evident, and the browser will take it as it is.
<p> The paragraph is a base element in ... </p>
<p> This places a empty line above and ... </p>

Display


The paragraph is a base element in text editing.The tag for a paragraph is <p>.
He places a empty line above and under the text to make it evident,
and the browser will take it as it is


HTML - Paragraph, justify

Paragraphs can be edited almost the same easy way as in a text editor. The next example is made with the help of the justify attribute
<p align="justify">The paragraph is a base element in ...</p>

Display

The paragraph is a base element in text editing.The paragraph is a base element in text editing.The tag for a paragraph is <p>. his one places a empty line above and under the text to make it evident, and the browser will take it as it is.

HTML - Centered paragraph

<p align="center">The paragraph is a base element in ...</p>

Display

The paragraph is a base element in text editing.The paragraph is a base element
in text editing.The tag for a paragraph is <p>.
his one places a empty line above and under the text to make it evident,
and the browser will take it as it is.

In this example every line of the paragraph has been centered in the middle of the giving back line.

HTML - The paragraph aligned to the right, right

<p align="right">The paragraph is a base element in ... </p>

Display

The paragraph is a base element in text editing.The paragraph is a base element in text editing.The tag for a paragraph is <p>. his one places a empty line above and under the text to make it evident, and the browser will take it as it is.

All the lines of the paragraph had been arranged, in the upper example, to the right.

References from http://www.tutorialehtml.com

HTML attributes 1

The attributes are used to personalize tags. What do i mean ? Somehow , someday you want to resize a image or a table or to change a font color. All these are possible with the help of the attributes.
The most tags have their own attributes. We will talk about this as we include in our talking a new tag. But now we will talk about a set of generic attributes which can be used with the majority of the tags.
Attributes are putted between the angular brackets (<>) of the opening tag.

THe "class" and "id" attributes in HTML

These two attributes are mostly the same. They have no straight role in the formatting of the elements but they are useful behind the scene with the help of CSS. We will talk their role at the right time when we study their syntax and their function in CSS.
The idea is that when you want to set a class of tags for being used later with the help of CSS, you can make the difference between two identical tags but with different attributes. Take a look at the next example :
<p id="italicparagraph">Paragraph type 1, italic </p>
<p id="boldparagraph">Paragraph type 2, bold </p>

Display

Paragraph type 1, italic
Paragraph type 2, bold

HTML - The "name" attribute

"name" is a bit different from "id" and "class". If you give a name to an element this one becomes a script variable for Javascript, ASP and PHP. Something that is very often meet in formulations and other interactive text fields.
<input type="text" name="TextField" />

Display


This attribute has no effect over the display of the text box, even if in the background it plays a very important role.

HTML - "title" attribute

This attribute is used rarely . He adds a title (a pop-up) to every element`s content. This attribute should not be forgotten. You can name almost everything however you want. The visualization appears when you need to stop with your mouse for a few second above the content.
 
<h2 title="I am a title attribute!!">A random title</h2>

Display

A random title


Pass with your mouse over the upper title so that you see the magic of the"title" attribute.This attribute will give yo your site a bit of interaction for those who visit you. Do not pass over this detail.

HTML - "align" attribute

If you want to align in a different way some elements of your page then you have at your disposition the "align" attribute. You can align to the left, right or the center of the page almost every element. By default elements will align to the left, excepting when it is specified an other alignment.
<h2 align="center">Centered title </h2>

Display

Centered title

Other examples:

<h2 align="left">Title aligned to the left </h2>
<h2 align="center">Centered title </h2>
<h2 align="right">Title aligned to the right </h2>

Display

Title aligned to the left

Centered title

Title aligned to the right


The default values for the attributes

Most of the tags are attributed standard attributes. This means that if you do not specify an other attribute, the browser will do it for you. For example a paragraph will align by himself to the left, excepting when you specify in an other way; the same is with a table. As long as you practice you will understand many more things about the default values of some tags.


Generic attributes

You must keep in your mind that attributes are used to design the elements of
a web page. I have put here together some of the most commune attributes used in HTML :

Attribute Options Function
align right, left, center Horizontal alignment
valign top, middle, bottom Vertical alignment
bgcolor numerical, hexadecimal, or RGB value A background behind an element.
background URL An image behind an element.
id Defined by user Names an element which will be used
with CSS.
class Defined by user Classifies un element which will be


used with CSS
width Numerical value Specifies the width of a table, image
or table box.
height Numerical value Specifies the height of an table,
title Defined by user "pop-up" a title for an element

References from http://www.tutorialehtml.com

Html tags

A browser read absolutely all you write in the HTML document. Tags have three parts as I said before, the opening, closing and content.
As you will learn there are hundreds of HTML tags. Absolutely all the elements that will be displayed by a browser need a tag or two.

<openingtag>Content</closingtag>
<p>Paragraph</p>

Tags are written in small letters. This is the standard of writing in XHTML, and Dynamic HTML.

Above are some examples of tags in HTML.
<body> acts as a capsule on the content.
<p>paragraph</p>

<h2>Title (heading)< h2>

<i>italic</ i>
<b>bold</b>
</body>

Exceptions - tags that do not require closing tag

There are some tags that do not meet the model shown above.The reason is that in fact these tags do not have content. Due to this fact we will use a modified manner of writing these tags.
The simplest example is "linebreak"
<br/>

This tag is combining the two tags, opening and closing.This way is more efficient to use. Line break is used to tell the browser that we want to get down a line below, but not closing paragraph.
Following this example, other tags have been modified to be write in a simpler and quicker way.

<img src="../img/image.jpg" /> -- Image Tag --
<br /> -- Line Break Tag --
<input type="text" size="12" /> -- Input Field --

Display




-- Line Break --

As you can see the browser is able to reproduce the image as long as we provide the location using the attribute "scr". More about this in the next tutorial.

References from http://www.tutorialehtml.com

HTML Elements

HTML elements have many ranks.All you see : paragraphs, ZiZix`s banner, the
navigation links from the left side and the right side , all are elements of this page.
An element has three parts : an opening tag, element`s content and an closing.
  1. <p> - the tag that opens a paragraph
  2. Element`s content - the paragraph itself.
  3. </p> - the closing tag.
***Note:
All the web pages will have at least the base elements: html, head, title and body.
--------------------------------------------------------------------------------------------------------------------------

<html>element...</html>

An HTML document will always begin and end with a <html> tag and respective
</html>. This is the standard structure of an HTML.

Please open a Notepad and copy the next cod :
<html>
</html>

Save the document from the File/Save As menu. Select All Files and name the new created file "index.html".Hit Save.Now open the file in a browser so that you have the possibility to refresh the page (F5).
If you did everything well you will be able to see your web page white !

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

<head> element

The <head> element is the one that is next. While you put in between html and body everything should be just fine."Head" has no visible function, so we will talk about this aspect in the next tutorials. Even though i want to mention that <head> can offer to the browser very useful informations.You can introduce here other cods like Javascript or CSS.
For the moment we will let the notions like that with the exception of the act that we introduce the next element from the list, but first lets take a look without him:
<html>
<head>
</head>
</html>

If you save the document and try to refresh the initial page from the browser you will not see any difference. Just have a little patience, because next we will study some visible elements.

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

<title> element

So that every thing goes well you must put the title tag inside the head tag. what you write between those two title tags ( <title> and </title>) will be seen as browser`s name, usually in the upper right side. Next we have the source code:
<html>
<head>
<title> My first web page!</title>
</head>
</html>

Now save the file and open it in your browser. You will be able to see the title
in the upper left or right side as like the majority of the browsers.
You can put any name you want, just remember that the descriptive names are the ones that are easier to find later.

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

<body> element

The body element is the one that defines the beginning of the itself page`s content (titles, paragraphs, photos, music and any all the others).As you can see in the menu from the left, we debate one by one all these content elements.
For now all you need to keep in your mind is that body covers all the content of the page.
<html>
<head>
<title> My first web page!</title>
</head>
<body>
Hey guys! Here we will put the content later !
</body>
</html>

Now see what you have done, and after i invite you to see what is in the next section.

References from http://www.tutorialehtml.com

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More