Search This Blog

  • ()
  • ()
Show more

PHP Tutorial - PHP Introduction

» PHP scripts are generally saved with the file extension .php . Statement The basic unit of PHP code is called a statement, which ends with...

»


PHP scripts are generally saved with the file extension .php.

Statement


The basic unit of PHP code is called a statement, which ends with a semicolon.

Usually one line of code contains just one statement, but we can have as many statements on one line as you want.

PHP Opening and Closing Code Islands


<?php and ?> marks the PHP code island.

The short tags version is <? and ?>.

<?="Hello, world!" ?>

Here is the equivalent, written using the standard open and closing tags:


<?php
print "Hello, world!";
?>


Example - PHP Statements


The following PHP code uses print statement to output message on to the screen.


<?php
// option 1
print "Hello, ";
print "world!";

// option 2
print "Hello, "; print "world!";
?>


The code above generates the following result.



echo is another command we can use to output message. echo is more useful because you can pass it several parameters, like this:


<?php
echo "This ", "is ", "a ", "test.";
?>


The code above generates the following result.



To do the same using print, you would need to use the concatenation operation (.) to join the strings together.





PHP Variables


A variable is a container holding a certain value.





Syntax


Variables in PHP beginning with $followed by a letter or an underscore, then any combination of letters, numbers, and the underscore character.

Here are the rules we would follow to name variables.

  • Variable names begin with a dollar sign ( $ )

  • The first character after the dollar sign must be a letter or an underscore

  • The remaining characters in the name may be letters, numbers, or underscores without a fixed limit







Example - Define PHP variables


We cannot start a variable with a number. A list of valid and invalid variable names is shown in the following table.





































VariableDescription
$myvarCorrect
$NameCorrect
$_AgeCorrect
$___AGE___Correct
$Name91Correct;
$1NameIncorrect; starts with a number
$Name'sIncorrect; no symbols other than "_" are allowed

Variables are case-sensitive. $Foois not the same variable as $foo.

Variable substitution


In PHP we can write variable name into a long string and PHP knows how to replace the variable with its value. Here is a script showing assigning and outputting data.


<?php
$name = "java2s.com";
print "Your name is $namen";
$name2 = $name;
print 'Goodbye, $name2!n';
?>


The code above generates the following result.



PHP will not perform variable substitution inside single-quoted strings, and won't replace most escape characters.

In the following example, we can see that:

  • In double-quoted strings, PHP will replace $name with its value;

  • In a single-quoted string, PHP will output the text $name just like that.




<?php
$food = "grapefruit";
print "These ${food}s aren't ripe yet.";
print "These {$food}s aren't ripe yet.";
?>



The code above generates the following result.



The braces {} tells where the variable ends.

COMMENTS

Name

apple,2,Article,14,At home,13,Author,14,Beauty,1,Biography,2,blackberry,1,Business,6,Cars,5,Celebrity,13,conspiraci,15,Fashion,5,galaxy,1,Gallery,1,Games,11,google,1,Hair,1,Health amp; Fitness,2,Histori,11,Home,169,HOSTING,27,HTML,7,imac,1,Image,1,iphone,1,Itcyber,7,Kuran,31,Lajme,34,Life amp; Love,1,Makeup amp; Skincare,1,mobile,5,monitor,1,Movies,2,News,5,No category,5,Photography,6,PHP,30,Poezi,24,POEZI amp; TEKSTE,2,Post,14,PROGRAMMIM,46,Relationships,1,review,6,samsung,1,Seo,7,Softvare,4,Sport,5,Sports,7,Stars,5,Tag,7,Tags,7,Tech,30,Teknologji,37,THEMES,3,Tutorials,191,Video,47,Videos,4,Vip News,2,Webhosting,67,WordPress,7,World,13,
ltr
item
Cr337: PHP Tutorial - PHP Introduction
PHP Tutorial - PHP Introduction
http://www.java2s.com/Tutorials/PHPImage/myResult/E/EXAMPLE_PHP_STATEMENTS__54C98699137E7D0CB632.PNG
Cr337
https://cr337.blogspot.com/2016/03/php-tutorial-php-introduction.html
https://cr337.blogspot.com/
http://cr337.blogspot.com/
http://cr337.blogspot.com/2016/03/php-tutorial-php-introduction.html
true
5516280490839897951
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy