Hello World<?php
echo 'Hello World!'; ?php> save this file as index.php and upload it directly to the server PHP 101
Let's quickly go through that is going on with the code Hello World. <?php - this tells the server to open execute php script. echo 'Hello World!' - Hello World message appears on the screen. ?php> -ends the execution of php script There is also PRINT command. What is the difference? PRINT returns a value since PRINT acts like a function print(). The whole idea behind PRINT is you can assign a variable to PRINT() You could end up setting up something like this $helloWorld = print "Hello World"; Echo does not return a value but it takes several arguments. Try this echo "Welcome", " ", "Mate", "!"; print "Welcome", " ", "Mate", "!"; Echo is valid but Print is not. As other languages you can concatenate two strings. $fullName="Alexander the Great"; $country = "Greece"; $fullName="Alexander the Great". ' '."is from".' '. $country; echo $fullName; echo "<br>"; $fullName="Alexander the Great"; $country = "Greece"; $fullName1 ="$fullName is from $country"; echo $fullName1; echo "<br>"; |
Variables
Variables in PHP are extremely similar to the ones in C++. There are some differences all PHP variables start with $ (dollar sign) $score; Just like in C++ we can assign a value to a variable $score=5; $myString = "Student!"; $modString="Hello, $myString!"; echo $modString; you should see "Hello, Student!" Let's say you have a double quote situation, for example "title"... use single quote Here is an example $string_title = 'My favorite movie is \"title"'; echo $string_title; |
Types of VarsBoolean - true/false - $myBool = true;
Int- interger (whole number) - $myInt=1; Float - a number with decimals - $myFloat=3.3; String - a quote - $myString = "Hello, my world!" Increments/DecrementsAgain, C++ and PHP have the same rule on increments and decrements.
var $a=5; $a=$a+1; result: 6 $a++; result: 7 $a--; result: 6 Pre-Increment and Pre-Decrement. var $a=5; var $newA=(--$a)+4; Lemme explain this before telling you the answer Because of (--$a) which is a pre-decrement, so variable $a loses 1 before the addition happens. Therefore, it is 4 + 4 = 8 not 9! Constantsall constants are recommended to be capitalized.
<? php define ["TEST', "Hello World!", true]; echo TEST; ?> Please take a note that even if you will assign another value to a variable TEST, the value "Hello World!" will not change. |
Testing the VariablesLet's say you have an integer and a float..
$total = $numOne + $numTwo; You will have a float as a result...We can always test what type of variable you have by using var_dump <?php $numOne=1; $numTwo=4.6; $numThree="Hello!"; ?> The way you can test to see what kind of variable you have, all you need to do is open developer console and go to prompt console and type var_dump ($numOne); Your output will be int(1) LengthLet's say you want to find what is the length of your string.
Replace StringTake the above variable $test = " Hello my World!";
add str_replace after echo echo str_replace ('world' , 'kitties', $test); existent - new - where also you could assign echo str_replace ('world' , 'kitties', $test); to a variable $replaceString = str_replace ('world' , 'kitties', $test); echo $replaceString; echo str_word_count ($test); |