Home >Backend Development >PHP Tutorial >2. Getting Started with PHP_PHP Tutorial

2. Getting Started with PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 16:08:46870browse


2. Getting Started with PHP

The online tutorials on the PHP site are already great. There are also links to some other tutorials there. This part of the article will make you familiar with PHP. It's impossible for me to miss anything. My purpose is only to allow you to quickly start your PHP programming.

2.1 First Conditions

You must first have a working web server that supports PHP. I assume that all PHP files on your server have the extension .php3.

2.2 PHP installation

Generate a file named test.php3 with the following content:

Then in your Open this file in your browser. Take a look at this page to see what options your PHP installation uses.

2.3 Syntax

As mentioned before, you can mix your PHP code and HTML code. So you have to have a way to differentiate between the two. Here are a few ways you can do it. You can pick whichever one you're most comfortable with and stick with it!

Separate from HTML

Here are the methods you can use:



<% . . . %>

Statement

is the same as Perl and C, Use (;) to separate statements in PHP. Those separate tags from HTML also indicate the end of a statement.

Comments

PHP supports C, C++ and Unix style comments:

/* C, C++ style multi-line comments */
// C++ style single line Comments
# Unix-style single-line comments

Hello, World!

With the knowledge we have learned, you can write the simplest program to output one of perhaps the most famous names in the programming world Words:


<br><?<br>echo "Hello World!";<br>?><br>< ;/TITLE><br></HEAD><br><BODY><br><H1><br>First PHP page<br></H1><br><HR><br> <?<br>// Single line C++ style comment<br>/*<br>printing the message<br>*/<br>echo "Hello World!";<br># Unix style single line comment<br>?><br></BODY><br></HTML><br><br>2.4 Data Types<br><br>PHP supports integers, floating point numbers, strings, arrays and objects. Variable types are usually not determined by the programmer but by the PHP runtime (what a relief!). But the type can also be set explicitly by the function cast or settype(). <br><br>Numeric value<br><br>The numeric value type can be an integer or a floating point number. You can use the following statement to assign a value to a value: <br>$a = 1234; # Decimal number <br>$a = -123; # Negative number <br>$a = 0123; # Octal number (equal to decimal number 83)<br>$a = 0x12; # Hexadecimal number (equal to 18 decimal numbers)<br>$a = 1.234; # Floating point number "double precision number" <br>$a = 1.2e3; # Double Exponential form of a precision number<br><br>String<br><br>Strings can be defined by fields enclosed in single or double quotes. Note that the difference is that strings enclosed in single quotes are defined literally, while strings enclosed in double quotes can be expanded. Backslash () can be used to separate certain special characters. For example: <br>$first = 'Hello';<br>$second = "World";<br>$full1 = "$first $second"; # Generate Hello World<br>$full2 = '$first $ second';# produces $first $second<br><br> can connect characters and numbers using arithmetic symbols. Characters are converted to numbers using their original position. There are detailed examples in the PHP manual. <br><br>Arrays and Hash Tables <br><br>Arrays and hash tables are supported in the same way. How you use them depends on how you define them. You can define them using list() or array(), or assign values ​​to arrays directly. The index of the array starts from 0. Although I haven't explained it here, you can easily use multidimensional arrays.<br><br>//An array containing two elements<br>$a[0] = "first"; <br>$a[1] = "second"; <br>$a[] = " third"; // A simple way to add array elements<br>// Now $a[2] is assigned the value "third"<br>echo count($a); // Print out 3 because the array has 3 elements<br>//Use one statement to define an array and assign the value<br>$myphonebook = array (<br>"sbabu" => "5348",<br>"keith" => "4829",<br>"carole" => "4533"<br>);<br>// Oh, forget about the dean, let’s add an element<br>$myphonebook["dean"] = "5397";<br>// The carale element you defined is wrong, let’s correct it <br>$myphonebook["carole"] => "4522"<br>// Haven’t I told you how to use similar support for arrays? ? Let's take a look at <br>echo "$myphonebook[0]"; // sbabu<br>echo "$myphonebook[1]"; // 5348 <br><br>Some others useful for arrays or hash tables The functions include sort(), next(), prev() and each(). <br><br>Object <br><br>Use the new statement to generate an object: <br>class foo <br>{<br>function do_foo () <br>{ <br>echo "Doing foo."; <br>}<br>}<br>$bar = new foo;<br>$bar->do_foo();<br><br>Change variable type<br><br> mentioned in the PHP manual : "PHP does not support (and does not require) defining the variable type directly when declaring the variable; the variable type will be determined based on the situation in which it is used. If you assign the variable var to a string, then it becomes a string. If you assign an integer value to it, then it becomes an integer. "<br>$foo = "0"; // $foo is a string (ASCII 48)<br>$foo++; // $foo. is the string "1" (ASCII 49)<br>$foo += 1; // $foo is now an integer (2)<br>$foo = $foo + 1.3; // $foo is a double ( 3.3)<br>$foo = 5 + "10 Little Piggies"; // $foo is an integer (15)<br>$foo = 5 + "10 Small Pigs"; // $foo is an integer (15) <br><br>If you want to forcefully convert the variable type, you can use the same function settype() as in C language. <br><br>2.5 Variables and Constants<br><br>You may have noticed that variables are prefixed with a dollar sign ($). All variables are local variables. In order to use external variables in the defined function, use the global statement. And if you want to limit the scope of the variable to the function, use the static statement. <br>$g_var = 1 ; // Global scope <br>function test() <br>{<br>global $g_var; // This way you can declare global variables <br>}<br><br>More More advanced is the variable representation of variables. Please refer to the PHP manual. This can sometimes be useful. <br><br>PHP has many built-in defined variables. You can also use the define function to define your own constants, such as define("CONSTANT", "value"). <br><br>2.6 Operators <br><br>PHP has the commonly seen operators in C, C++ and Java. The precedence of these operators is also consistent. Assignment also uses "=". <br><br>Arithmetic and characters<br><br>Only one of the following operators is related to characters: <br>$a + $b: Add<br>$a - $b: Subtract<br>$ a * $b : Multiply <br>$a / $b : Divide <br>$a % $b : Modulo (remainder) <br>$a . $b : String concatenation <br><br> logical sum Comparing <br><br> logical operators are: <br>$a <br> $b: or <br>$a or $b: or <br>$a && $b: with <br>$a and $b: with <br>$a xor $b: exclusive or (true when $a or $b is true, false when both are the same) <br>! $a: non-<br> comparison operators are : <br>$a == $b : equal <br>$a != $b : not equal <br>$a < $b : less than <br>$a <= $b : less than or equal to <br>$a > $b : greater than <br> $a >= $b : greater than or equal to <br> Like C, PHP also has a triple operator (?:). Bit operators also exist in PHP. <br><br>Priority<br><br>Just like C and Java! <br><br>2.7 Control flow structure<br><br>PHP has the same flow control as C. I will briefly introduce it below.<br><br>if, else, elseif, if(): endif<br><br>if (expression one) <br>{<br>. . .<br>} <br>elseif (expression 2) <br>{<br>. . .<br>} <br>else <br>{<br>. . .<br>}<br>// Or like Python <br>if (expression 1) :<br>. . .<br>. . .<br>elseif (Expression 2) :<br>. . .<br>else :<br>. .<br>endif ;<br> <br>Loops. while, do..while, for <br><br>while (expression) <br>{<br>. . .<br>}<br>do <br>{<br>. . .<br>} <br>while (expression);<br>for (expression one; expression two; expression three) <br>{<br>. . .<br>}<br>/ / Or like Python <br>while (expr) :<br>. . .<br>endwhile ;<br><br>switch<br><br>switch is the best for multiple if-elseif-else structures Replacement: <br>switch ($i) <br>{<br>case 0:<br>print "i equals 0";<br>case 1:<br>print "i equals 1";<br> case 2:<br>print "i equals 2";<br>}<br><br>break, continue<br><br>break breaks the current loop control structure. <br>continue is used to jump out of the remaining current loop and continue executing the next loop. <br><br>require, include<br><br> is like #include preprocessing in C. The file you specify in require will replace its location in the main file. When referencing a file conditionally, you can use include(). This allows you to split complex PHP files into multiple files and reference them separately when needed. <br><br>2.8 Function <br><br>You can define your own function like the following example. The return value of the function can be any data type: <br>function foo (variable name one, variable name two, . . . , variable name n) <br>{<br>echo "Example function.n";<br> return $retval;<br>}<br>All PHP code can appear in function definitions, even definitions of other functions and classes. Functions must be defined before being referenced. <br><br>2.9 Classes<br><br>Use class models to create classes. You can refer to the detailed explanation of classes in the PHP manual. <br>class Employee <br>{<br>var $empno; // Number of employees <br>var $empnm; // Employee name <br><br>function add_employee($in_num, $in_name)<br>{ <br>$this->empno = $in_num;<br>$this->empnm = $in_name;<br>}<br><br>function show() <br>{<br>echo "$ this->empno, $this->empnm";<br>return;<br>}<br><br>function changenm($in_name)<br>{<br>$this->empnm = $ in_name;<br>}<br>}<br><br>$sbabu = new Employee;<br>$sbabu->add_employee(10,"sbabu");<br>$sbabu->changenm(" babu");<br><br>$sbabu->show();<br> </p> <p align="left"></p> <div style="display:none;"> <span id="url" itemprop="url">http://www.bkjia.com/PHPjc/314694.html</span><span id="indexUrl" itemprop="indexUrl">www.bkjia.com</span><span id="isOriginal" itemprop="isOriginal">true</span><span id="isBasedOnUrl" itemprop="isBasedOnUrl">http: //www.bkjia.com/PHPjc/314694.html</span><span id="genre" itemprop="genre">TechArticle</span><span id="description" itemprop="description">2. Getting Started with PHP The online tutorials for PHP sites are already great. There are also links to some other tutorials there. This part of the article will make you familiar with PHP. I can't do it without any legacy...</span> </div> <div class="art_confoot"></div></div><div class="nphpQianMsg"><div class="clear"></div></div><div class="nphpQianSheng"><span>Statement:</span><div>The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn</div></div></div><div class="nphpSytBox"><span>Previous article:<a class="dBlack" title="ASP Knowledge Lecture 4_PHP Tutorial" href="https://m.php.cn/faq/312267.html">ASP Knowledge Lecture 4_PHP Tutorial</a></span><span>Next article:<a class="dBlack" title="ASP Knowledge Lecture 4_PHP Tutorial" href="https://m.php.cn/faq/312269.html">ASP Knowledge Lecture 4_PHP Tutorial</a></span></div><div class="nphpSytBox2"><div class="nphpZbktTitle"><h2>Related articles</h2><em><a href="https://m.php.cn/article.html" class="bBlack"><i>See more</i><b></b></a></em><div class="clear"></div></div><ins class="adsbygoogle" style="display:block" data-ad-format="fluid" data-ad-layout-key="-6t+ed+2i-1n-4w" data-ad-client="ca-pub-5902227090019525" data-ad-slot="8966999616"></ins><script> (adsbygoogle = window.adsbygoogle || []).push({}); </script><ul class="nphpXgwzList"><li><b></b><a href="https://m.php.cn/faq/1.html" title="How to use cURL to implement Get and Post requests in PHP" class="aBlack">How to use cURL to implement Get and Post requests in PHP</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/faq/1.html" title="How to use cURL to implement Get and Post requests in PHP" class="aBlack">How to use cURL to implement Get and Post requests in PHP</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/faq/1.html" title="How to use cURL to implement Get and Post requests in PHP" class="aBlack">How to use cURL to implement Get and Post requests in PHP</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/faq/1.html" title="How to use cURL to implement Get and Post requests in PHP" class="aBlack">How to use cURL to implement Get and Post requests in PHP</a><div class="clear"></div></li><li><b></b><a href="https://m.php.cn/faq/2.html" title="All expression symbols in regular expressions (summary)" class="aBlack">All expression symbols in regular expressions (summary)</a><div class="clear"></div></li></ul></div></div><ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5902227090019525" data-ad-slot="5027754603"></ins><script> (adsbygoogle = window.adsbygoogle || []).push({}); </script><footer><div class="footer"><div class="footertop"><img src="/static/imghwm/logo.png" alt=""><p>Public welfare online PHP training,Help PHP learners grow quickly!</p></div><div class="footermid"><a href="https://m.php.cn/about/us.html">About us</a><a href="https://m.php.cn/about/disclaimer.html">Disclaimer</a><a href="https://m.php.cn/update/article_0_1.html">Sitemap</a></div><div class="footerbottom"><p> © php.cn All rights reserved </p></div></div></footer><script>isLogin = 0;</script><script type="text/javascript" src="/static/layui/layui.js"></script><script type="text/javascript" src="/static/js/global.js?4.9.47"></script></div><script src="https://vdse.bdstatic.com//search-video.v1.min.js"></script><link rel='stylesheet' id='_main-css' href='/static/css/viewer.min.css' type='text/css' media='all'/><script type='text/javascript' src='/static/js/viewer.min.js?1'></script><script type='text/javascript' src='/static/js/jquery-viewer.min.js'></script><script>jQuery.fn.wait = function (func, times, interval) { var _times = times || -1, //100次 _interval = interval || 20, //20毫秒每次 _self = this, _selector = this.selector, //选择器 _iIntervalID; //定时器id if( this.length ){ //如果已经获取到了,就直接执行函数 func && func.call(this); } else { _iIntervalID = setInterval(function() { if(!_times) { //是0就退出 clearInterval(_iIntervalID); } _times <= 0 || _times--; //如果是正数就 -- _self = $(_selector); //再次选择 if( _self.length ) { //判断是否取到 func && func.call(_self); clearInterval(_iIntervalID); } }, _interval); } return this; } $("table.syntaxhighlighter").wait(function() { $('table.syntaxhighlighter').append("<p class='cnblogs_code_footer'><span class='cnblogs_code_footer_icon'></span></p>"); }); $(document).on("click", ".cnblogs_code_footer",function(){ $(this).parents('table.syntaxhighlighter').css('display','inline-table');$(this).hide(); }); $('.nphpQianCont').viewer({navbar:true,title:false,toolbar:false,movable:false,viewed:function(){$('img').click(function(){$('.viewer-close').trigger('click');});}}); </script></body><!-- Matomo --><script> var _paq = window._paq = window._paq || []; /* tracker methods like "setCustomDimension" should be called before "trackPageView" */ _paq.push(['trackPageView']); _paq.push(['enableLinkTracking']); (function() { var u="https://tongji.php.cn/"; _paq.push(['setTrackerUrl', u+'matomo.php']); _paq.push(['setSiteId', '9']); var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s); })(); </script><!-- End Matomo Code --></html>