{"id":165,"date":"2009-07-15T07:26:34","date_gmt":"2009-07-15T11:26:34","guid":{"rendered":"http:\/\/awgentry.com\/blog\/?p=165"},"modified":"2009-07-15T07:26:34","modified_gmt":"2009-07-15T11:26:34","slug":"mini-php-crash-course","status":"publish","type":"post","link":"http:\/\/awgentry.com\/weblog\/mini-php-crash-course\/","title":{"rendered":"Mini PHP Crash Course"},"content":{"rendered":"<div class=\"zemanta-img\">\n<div style=\"width: 166px\" class=\"wp-caption alignright\"><a href=\"http:\/\/commons.wikipedia.org\/wiki\/File:HTML.svg\"><img loading=\"lazy\" decoding=\"async\" class=\" \" title=\"A graphical despiction of a very simple html d...\" src=\"http:\/\/upload.wikimedia.org\/wikipedia\/commons\/thumb\/8\/84\/HTML.svg\/266px-HTML.svg.png\" alt=\"A graphical despiction of a very simple html d...\" width=\"160\" height=\"186\" \/><\/a><p class=\"wp-caption-text\">Image via Wikipedia<\/p><\/div>\n<\/div>\n<p>If you&#8217;re a Web coding geek like me, you&#8217;ve probably mastered <a href=\"http:\/\/www.w3schools.com\/Xhtml\/\">(X)HTML<\/a>, used some <a href=\"http:\/\/www.webmonkey.com\/reference\/Stylesheets_Guide\">CSS<\/a> and <a href=\"http:\/\/www.javascriptsource.com\">JavaScript<\/a> &#8212; and you&#8217;re looking for the next step up. You may have realized that <a href=\"http:\/\/www.php.net\">PHP programming<\/a> is an important language to learn. <em>(Note: If this alphabet soup is Greek to you, go back to\u00a0<a href=\"http:\/\/www.w3schools.com\/Xhtml\/xhtml_intro.asp\">(X)HTML 101<\/a> before continuing.) <\/em>Instead of being a rather simple markup or <a class=\"zem_slink\" title=\"Scripting language\" rel=\"wikipedia\" href=\"http:\/\/en.wikipedia.org\/wiki\/Scripting_language\">scripting language<\/a>,<strong> PHP can be the foundation for some amazing Web applications. <\/strong><em><\/em><\/p>\n<p>So you wanna&#8217; try it yourself, eh? Good news:  It&#8217;s not too hard to learn the basics. Here&#8217;s a <strong>super-quick tutorial<\/strong> to get you rolling. <strong>Fire up a simple <a class=\"zem_slink\" title=\"Text editor\" rel=\"wikipedia\" href=\"http:\/\/en.wikipedia.org\/wiki\/Text_editor\">text editor<\/a><\/strong> like <em>Notepad<\/em>, or a code editor such as\u00a0 <a href=\"http:\/\/www.scintilla.org\/SciTE.html\">SciTE<\/a> <em>(free)<\/em>, and get ready for your first taste of PHP-coding goodness!<\/p>\n<p><strong>PHP<\/strong> stands for &#8220;<em><a class=\"zem_slink\" title=\"PHP\" rel=\"homepage\" href=\"http:\/\/www.php.net\/\">PHP: Hypertext Preprocessor<\/a><\/em>.&#8221; (Yeah, codeheads can be odd.) It&#8217;s written inside\u00a0 <strong>&lt;?php<\/strong> and\u00a0 <strong>?&gt;<\/strong> tags, as you&#8217;ll see below. One way to keep the code easy-to-read is to put these tags on their own line. Paste the following code into the body of a regular HTML document:<strong><br \/>\n&lt;?php<br \/>\necho &#8220;Spiffiness Ensues&#8221;;<br \/>\n<\/strong><strong>?&gt;<\/strong><em><\/em><\/p>\n<p>The code can also be written in a single line, e.g.,<strong><br \/>\n&lt;?php\u00a0 echo &#8220;Spiffiness Ensues&#8221;; ?&gt;<\/strong><em><\/em><\/p>\n<p><em>Echo<\/em> basically means &#8220;print this&#8221;. Don&#8217;t forget the semicolon(;) at the end of the line, <em>outside<\/em> the quotes. PHP is very picky about this kind of thing.<\/p>\n<p><strong>PHP statements play nice with HTML code<\/strong> on the same line. For example, if you want to print the title of your webpage using PHP, you can write:<br \/>\n<strong>&lt;title&gt;&lt;?php echo &#8220;Spiffiness Ensues&#8221;; ?&gt;&lt;\/title&gt;<br \/>\n<\/strong><\/p>\n<p>&#8230; and the browser will happily oblige you.<\/p>\n<p>Now, <strong>save your document as a <em>.PHP<\/em> file<\/strong> &#8212; that is, a text file with a .PHP extension, e.g., <strong>mypage.php <\/strong>. Then open the file in your local Web browser. You should see&#8230; nothing. Nope, sorry, it won&#8217;t work that way. <strong>PHP needs to be installed<\/strong> on your computer or server for it to work right, which is a whole new can of worms.<\/p>\n<div id=\"attachment_174\" style=\"width: 328px\" class=\"wp-caption alignright\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-174\" class=\"size-full wp-image-174\" title=\"Basic PHP Example\" src=\"http:\/\/gentry.co\/wp-content\/uploads\/2009\/05\/phpcrashcourse3.jpg\" alt=\"Webpage, PHP, and source\" width=\"322\" height=\"600\" \/><p id=\"caption-attachment-174\" class=\"wp-caption-text\">Basic PHP page, with the original PHP (bottom) and the HTML the browser created from it.<\/p><\/div>\n<p>What&#8217;s a poor code monkey to do? One option is simply to <strong>upload it onto a server that has PHP installed<\/strong>, as most do. (If it doesn&#8217;t, you need to find one that does before you can publish your PHP files, anyway&#8230; A couple good choices are <a href=\"http:\/\/www.hostgator.com\">Hostgator<\/a> or <a href=\"http:\/\/www.startlogic.com\">Startlogic<\/a>.) Just upload it to the root directory, or wherever you fancy. When you type in the address, e.g., <strong>http:\/\/www._____.com\/mypage.php<\/strong> , it should show a page with &#8220;Spiffiness Ensues&#8221; written near the top. <em>Check out the example on the right.<\/em> Ta-da!<\/p>\n<p>The beauty of PHP is that <strong>anything you write gets processed into straight HTML before it reaches the browser<\/strong>. For the example above, for, uh, example, the HTML+PHP line becomes:<br \/>\n<strong>&lt;title&gt;Spiffiness Ensues&lt;\/title&gt; <\/strong><\/p>\n<p>That means that both search engines and users see a single, integrated webpage, in spite of the fact that the page itself may be composed of two or more PHP, HTML, and other files. (On the downside, it&#8217;s tricker to learn techniques of other PHP coders from studying their webpages&#8217; <a class=\"zem_slink\" title=\"Source code\" rel=\"wikipedia\" href=\"http:\/\/en.wikipedia.org\/wiki\/Source_code\">source code<\/a>, but that only affects true <a href=\"http:\/\/awgentry.com\/blog\/59\/propheads-and-umbrellas-huh\">propheads<\/a>.)<\/p>\n<p>For some more <strong>basic PHP guidance<\/strong>, check out the comprehensive resource at <strong><a href=\"http:\/\/www.php.net\">PHP.net<\/a><\/strong>. Some good items to start with are <a href=\"http:\/\/us.php.net\/manual\/en\/function.echo.php\">echo<\/a>, <a href=\"http:\/\/us.php.net\/manual\/en\/control-structures.if.php\">if statements<\/a>, <a href=\"http:\/\/us.php.net\/manual\/en\/control-structures.for.php\">for loops<\/a>, and the very versatile <a href=\"http:\/\/us.php.net\/manual\/en\/function.include.php\">include\/require<\/a>. Happy coding!<\/p>\n<h6 class=\"zemanta-related-title\">Related articles by Zemanta<\/h6>\n<ul class=\"zemanta-article-ul\">\n<li class=\"zemanta-article-ul-li\"><a href=\"http:\/\/www.brighthub.com\/internet\/web-development\/articles\/77890.aspx\">Using Cookies in PHP<\/a> (brighthub.com)<\/li>\n<li class=\"zemanta-article-ul-li\"><a href=\"http:\/\/www.webresourcesdepot.com\/40-must-bookmark-php-classes-libraries-for-developing-faster\/\">40+ Must-Bookmark PHP Classes &amp; Libraries For Developing Faster<\/a> (webresourcesdepot.com)<\/li>\n<li class=\"zemanta-article-ul-li\"><a href=\"http:\/\/r.zemanta.com\/?u=http%3A\/\/www.infoworld.com\/d\/developer-world\/php-development-comes-google-android-652%3F_infoworld_news&amp;a=21039878&amp;rid=172cfdb5-1f6c-4ffb-af93-aed5b59bc594&amp;e=e8da287642079caf9b3fa7b0ff32f3e5\">PHP development comes to Google Android<\/a> (infoworld.com)<\/li>\n<\/ul>\n<div class=\"zemanta-pixie\"><img decoding=\"async\" class=\"zemanta-pixie-img\" src=\"http:\/\/img.zemanta.com\/pixy.gif?x-id=172cfdb5-1f6c-4ffb-af93-aed5b59bc594\" alt=\"\" \/><span class=\"zem-script more-related pretty-attribution\"><script src=\"http:\/\/static.zemanta.com\/readside\/loader.js\" type=\"text\/javascript\"><\/script><\/span><\/div>\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;re a Web coding geek like me, you&#8217;ve probably mastered (X)HTML, used some CSS and JavaScript &#8212; and you&#8217;re looking for the next step up. You may have realized that PHP programming is an important language to learn. (Note: If this alphabet soup is Greek to you, go back to\u00a0(X)HTML 101 before continuing.) Instead&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[80,81,82,83],"class_list":["post-165","post","type-post","status-publish","format-standard","hentry","category-geek","tag-php","tag-programming","tag-servers","tag-tutorial"],"_links":{"self":[{"href":"http:\/\/awgentry.com\/weblog\/wp-json\/wp\/v2\/posts\/165","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/awgentry.com\/weblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/awgentry.com\/weblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/awgentry.com\/weblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/awgentry.com\/weblog\/wp-json\/wp\/v2\/comments?post=165"}],"version-history":[{"count":0,"href":"http:\/\/awgentry.com\/weblog\/wp-json\/wp\/v2\/posts\/165\/revisions"}],"wp:attachment":[{"href":"http:\/\/awgentry.com\/weblog\/wp-json\/wp\/v2\/media?parent=165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/awgentry.com\/weblog\/wp-json\/wp\/v2\/categories?post=165"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/awgentry.com\/weblog\/wp-json\/wp\/v2\/tags?post=165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}