If you create script, PHP script will be executed on some server, HTML page as a result will sent to the browser. You can't execute PHP, if you have no server. It is used with Apache web server. Suggested platform -Xampp, which contains mysql , php, apache and another useful stuff. PHP is a scripting language originally designed for producing dynamic web pages. More about history ande features here: It has evolved to include a command line interface....
PHP is a server-side scripting language for creating dynamic Web pages. You create pages with PHP and HTML. When a visitor opens the page, the server processes the PHP commands and then sends the results to the visitor's browser, just as with ASP or ColdFusion. Unlike ASP or ColdFusion, however, PHP is Open Source and cross-platform. PHP runs on Windows NT and many Unix versions, and it can be built as an Apache module and as a binary that can run as a CGI. When built as an Apache module, PHP is especially lightweight and speedy. Without any process creation overhead, it can return results quickly, but it doesn't require the tuning of mod_perl to keep your server's memory image small.
In addition to manipulating the content of your pages, PHP can also send HTTP headers. You can set cookies, manage authentication, and redirect users. It offers excellent connectivity to many databases (and ODBC), and integration with various external libraries that let you do everything from generating PDF documents to parsing XML.
Main PHP learning materials:
https://www.w3schools.com/php/default.asp
http://www.tizag.com/phpT/
Documentation:
http://php.net/docs.php
PART 1 - TESTING
Self learning - The first video about PHP - PHP Tutorial 1 - What is PHP?
https://www.youtube.com/watch?v=-oUXcTz1eEA
Watching the video in a lecture:
https://www.youtube.com/watch?v=50PURb6sfzo
Intro:
https://www.w3schools.com/php/php_intro.asp
We need Web server XAMPP:
XAMPP control panel -> launch Apache web server
1. Testing:
Open browser -> enter http://localhost or http://127.0.0.1
Location of your web site (here we will place our files):
c:\xampp\htdocs
2. Create files with extention .php, using some text editor (e.g. notepad++):
c:\xampp\htdocs\a.php
And run it (open) on browser:
http://localhost/a.php
3. First script:
<?php
echo "PHP:<hr />My first PHP script!";
?>
3a. Syntax
https://www.w3schools.com/php/php_syntax.asp
https://www.w3schools.com/php/php_comments.asp
4. There is a script which prints text:
<?php
echo "hello";
echo 'hello';
?>
5. The information about PHP and WWW server:
<?php
phpinfo();
?>
5a. Variables:
https://www.w3schools.com/php/php_variables.asp
6. Printing variables, difference between " " and ' ':
<?php
$k = 1;
echo "$k";
echo $k;
?>
<hr />
<?php
echo "$k $k <br />";
echo $k . $k . "<br />";
echo "$k" . "$k<br/>";
echo '$k' . '$k<br/>';
?>
7. Joining two variables:
<?php
# commment
$a = 'my shoes'; //+ comment
$b = 'is two';
echo $a . $b . "<br />";
?>
8. Another PHP script example. Joining two variables:
<?php
// error_reporting(E_ALL ^ E_NOTICE); // uncomment to disable notices
$a = 'mano batai'; // variables
$b = 'buvo du';
echo $A . $b . "<br />"; // printing
echo "$A $b <br />";
?>
9. Syntax errors:
<?php
echo "hello";
text
<hr />
----000
?>
text
<hr />
10. Defining constant:
<?php
define( "con", "value" );
echo con . "text"; // prints constant
echo "con"; // treats as simple text
?>
11 Defining constant. Environmental constants:
<?php
echo __FILE__ ;
echo "<br />" ;
echo __LINE__ ;
?>
11a. Data types:
https://www.w3schools.com/php/php_datatypes.asp
12. One line and multiline comments:
<h3>Text <?php // echo $c = "php"; ?> visible</h3>
<h3>Text <?php /* echo $c = "php"; ?> not visible</h1><i>text</i></h3><?php
*/
?>
12a. New line. Special characters:
<?php
//1
echo "<br />a
b
c
d<br />";
//2
echo "<br />a \n b \n c \n d \n <br />"; # new line
//3
echo "<pre> a \n b \n c \n d \n </pre>"; # new line, which is seen in a browser
//4
echo "\"\""; echo '""'; echo '\'\''; # special chars
//5
//echo """"; # an error, uncomment
?>
12b. PHP and HTML:
<?php
$a = 1 ;
echo "<input type = \"text\" value = \"$a\" />";
echo '<input type = "text" value = "$a" />';
echo '<input type = "text" value = "' . $a . '" />';
?>
12c. PHP and HTML:
<?php
$a = 1 ;
?>
<input type = "text" value = "<?php echo $a ; ?>" />
13. Operators and variables:
<?php
error_reporting(E_ALL ^ E_NOTICE);
$a = "10pigs";
$b = "1cow";
# The result is integer
echo $a + $b; echo "<br />";
# The result is integer
echo $a + true; echo "<br />";
# The result is integer
echo $a + false; echo "<br />";
# What will we get here, joining different types
echo $a . false; echo "<br />";
echo "text". $a . false ; "<br />";
?>
13a. Learning material:
14. The difference between dot and comma:
<?php
echo 'Row' , '1 ' , '2 ' , '3 ' , '4.' , "\n" ; echo "<br />";
echo 'Row' . '1 ' . '2 ' . '3 ' . '4.' . "\n" ; echo "<br />";
echo "2 + 2 = " . 2+2 ; echo "<br />"; # 4
echo "2 + 2 = " . 2 ; echo "<br />"; # 2+2=2
echo "2 + 2 = " , 2+2 ; echo "<br />"; # 2+2 = 4
echo "2 + 2 = " . (2+2) ; echo "<br />"; # 2+2 = 4
?>
15. Various expressions:
<?php
#1
$b = ++$a; //as well as --$a
echo "1: a=" . $a . ", b= " . $b . "<br />";
#2
$b = $a++;
echo "2: a=" . $a . ", b=". $b . "<br />"; # test $a--
#3
echo "3: remainder = " . 10%$a . "<br />";
#4
$b=3;
echo "4: b=" . "$b <br />";
# if $b, then $a = to first part "yes", or another
$a = ($b? "yes" : "no" );
echo "4: " . $a . "<br />";
# if $d, then $a = to first part "yes", or another
$a = ($d?"yes":"no");
echo "4: " . $a . "<br />";
#5
$a = ($b = 4) + 5;
echo "5: " . $a ." <br /> ". $b ;
?>
16. Conversion of datatypes. (int), (float), (bool), (string) and other:
<?php
# real, double and float number types represent the same data type
$foo =1.23234364231313131314343545431;
echo "foo value: $foo <br />";
# check data type
echo "foo type:" . gettype($foo)."<br /><br />";
# convert to float type
$foo = (float) $foo ;
# check data type
echo "foo to float: ". gettype($foo)."<br /><br />";
$foo = (bool)$foo;
echo "foo to bool: $foo, " . gettype($foo)."<br /><br />";
# assigning boolean value
$foo = faLSE;
echo "$foo " . gettype($foo)."<br /><br />";
# text
$foo ="my name";
echo "foo first element" . $foo[1] ."<br /><br />";
# Convert variable foo to an array
$foo = (array) $foo;
# print array element inside text.
echo "$foo ${foo[0]} ${foo[1]} <br /><br />";
echo "foo to array: " . gettype($foo)."<br /><br />";
# back to string
$foo = (string) $foo[0];
echo "back to string: $foo ${foo[0]} " . gettype($foo)."<br />";
Useful functions for checking data types
is_bool - logical
is_double is_float is_real - float numbers
is_int is_integer is_long - integer
is_null is_numeric - int, float
is_object - object
is_string - string
is_array - array
Recommended links for self learning:
If we want to format your script more beautiful: http://beta.phpformatter.com/
|