Docsity
Docsity

Prepara tus exámenes
Prepara tus exámenes

Prepara tus exámenes y mejora tus resultados gracias a la gran cantidad de recursos disponibles en Docsity


Consigue puntos base para descargar
Consigue puntos base para descargar

Gana puntos ayudando a otros estudiantes o consíguelos activando un Plan Premium


Orientación Universidad
Orientación Universidad


Introducing PHP, Monografías, Ensayos de Sistemas de Control No Lineal

This book is a comprehensive guide for developers who are new to php and non-programmers just starting out. It covers a wide range of topics, including a brief history of php, installing and configuring the language, basic php syntax, working with html, variables, numbers, dates, strings, control structures, databases, sessions, images, and security considerations. The book aims to provide a solid foundation in php programming, explaining the key concepts and features of the language in a clear and accessible manner. It includes numerous examples and exercises to help readers practice and apply what they've learned. Whether you're a seasoned developer looking to expand your skills or a complete beginner, this book offers a valuable introduction to the world of php and dynamic web development.

Tipo: Monografías, Ensayos

2014/2015

Subido el 20/07/2023

jose-pancorbo-luperdi
jose-pancorbo-luperdi 🇵🇪

1 documento

1 / 128

Toggle sidebar

Esta página no es visible en la vista previa

¡No te pierdas las partes importantes!

bg1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Vista previa parcial del texto

¡Descarga Introducing PHP y más Monografías, Ensayos en PDF de Sistemas de Control No Lineal solo en Docsity!

Table of Contents

Introduction Introduction What is PHP? Introducing PHP Example A little history Installing and Configuring PHP Introduction Windows Users Using Wampserver Mac Users How Do I Know it is Working? Oracle VM Virtual Box Installing for a Web Site Exercise Introduction to HTML Introduction Basic HTML Basic Elements of HTML Tags Nested Tags Required tags DocType Head Optional Tags Meta Tags Useful Tags Headers DIV Images/Picture

Useful String Functions Sources of Documentation Exercise How to Interpret PHP.NET documentation Return Value Function Name Parameters Dates Example: Calculating Age in Years Variable Scope Control Structures Introduction if if… else Exercise: Open Hours PHP Switch PHP Looping PHP while PHP for How to use a database, such as mySQL Introduction What are Databases? Getting Started with phpMyAdmin What is phpMyAdmin? Using phpMyAdmin to create a database Introduction to SQL Using phpMyAdmin to create a Table in a database Defining our first table What defines an automobile? Datatypes Numbers Characters

Dates Exercise: Create a Table Working with SQL Statements INSERT Statements SELECT Statements WHERE Statements Comparison Operators ORDER BY UPDATE Statements DELETE Statements Using mySQL and PHP Together Introduction Code! Code Listing: createdb.php Code Explained: createdb.php Hey, where’s the HTML? Creating forms to Display, Add, Edit, and Delete data Introduction Forms that Add Data to a Database A Basic Form HTML Code Form Action PHP Code A Brief Time Out…include files and SQL Injection Include Files SQL Injection Forms that Display Summary Data Code Code Explained Exercise: Tweaking the SELECT Improving the look of the table with CSS CSS Explained

Introduction Single Quotes vs Double Quotes The Equal Sign Comparison Operators Security Considerations Introduction Balancing Security and Usability SQL Injection Additional Resources Appendix A: Installing PHP on a Website How to install on a Windows Server How to install on a Linux Server Author’s Note A Note from one of my Kickstarter Backers….

- Pasha Kagan, soon-to-be PHP Developer

Introduction

Introduction

This book is for the developer who has just come across PHP and is wondering what the big deal is, and also for the non-programmer who is just starting out— and doesn’t know where to begin. As Confucius once wisely said, “I hear and I forget. I see and I remember. I do and I understand.” This book will get you doing. The book is presented as a case study of “Sam’s Used Cars,” and you’ll be building a web site for Sam’s business as we go along. Take the time and do the exercises. Struggle a little before you look up the answers. Speaking of the answers, the companion web site to this book is http://www.joyofphp.com where you can find all the code snippets (and answers to the exercises) from this book—plus some extra goodies. If you like the book, please locate it on Amazon.com and give it a favorable review. If you don’t like it, or find something that you think needs to be fixed, or you have an idea for “More Joy of PHP”, please email me at [email protected] What is PHP? introduces PHP in its many and varied contexts. It explains the difference between a PHP server, a PHP file, and PHP the language. It also describes the point of PHP, which is to create dynamic web pages. Installing and Configuring PHP describes how to install and configure PHP on your own computer. Of course, you can’t do much PHP programming if you don’t have PHP, so this is an important prerequisite to the rest of the book. Introduction to HTML lays the groundwork by discussing HTML. PHP is a language that modifies and generates HTML, so you have to know HTML as the foundation for using PHP to modify HTML. We cover required and optional tags, plus enough extras to get you going. Basic PHP Syntax Introduces the language of PHP. Here we show how to intersperse PHP and HTML in the same file, and what to expect when you do so. Some Fun Right Away gives you a chance to try out some PHP before we go much further, to give you an early sense of the joy you are going to experience when you master it. Editors and Staying Organized talks about how to actually edit a PHP file, and guides readers toward some of the tools available to make editing easy. Also, staying organized

What is PHP?

PHP is a programming language you can use to create web applications. It’s free, powerful, relatively easy to set up and learn, and it has extensions and frameworks available to do almost anything you could imagine. You can get started quickly, and you won’t outgrow it later when you get really good at it. In my humble opinion, PHP is a great language that will be well worth it the time and effort you put into learning it. Frankly, it’s just plain fun too. Let’s get started. The most basic concept you need to grasp is that a web page is just a bunch of text, organized in a certain way, which is displayed by a browser. Only a few companies make browsers, but millions of people make web pages—and so can you. Most computer programs need some way to know if a file is intended for them or not. In the PC world, this is accomplished by file extensions. (Bear with me, this next bit is relevant). For example, a file named “my book. docx ” is associated with Microsoft Word because its extension (the text following the dot) is “docx”. Similarly, a PowerPoint file might end with .ppt or .pptx. Other programs also have their own unique extensions. A web page typically, but not exclusively, has an extension such as .htm or .html to indicate that it is an HTML file. An HTML file can be on your own computer, or on a different computer somewhere out on the Internet. The browser doesn’t care. Here’s a simple example of an html file out on the Internet: http://www.tsowell.com/columns.htm

This was about the simplest web page I could find… just two links to other pages. Notice that the last four characters are .htm, which indicates that it is an HTML file intended for display in a browser. If that file were on your local computer, for instance in your “My Documents” folder, all you would have to do to see it in a browser would be to double-click on it. Your browser would know how to do the rest and you would see something like this:

This particular file is not on your local computer, however. It is on a server out on the Internet. So how does the file get into your browser when you click on it? At the risk of

oversimplifying it, you don’t have to worry about that part so much. The other computer, known as the “web server”, has the file and it knows how to get it to you. In the case of an HTML file such as this one, the server sends the file as is without doing anything to it. In other words, the file that the browser gets is exactly the same as the file on the file system, regardless of whether it was originally on your computer or on the server. We would call this a static web page.

Introducing PHP

Now let’s add PHP to the picture. PHP has several meanings depending on the context in which it is used, so I’m going to try to explain them all. There is a “ PHP server ”, which is a web server that is running PHP software on it. Let’s contrast a PHP server with a “plain” server, one that is not running PHP. A “plain” web server just takes a request from a browser, locates the appropriate file, and sends it to the browser as is , with no manipulation. In other words, it only serves static web pages. Once you add PHP to a web server, you get additional functionality—without taking any existing functionality away. The server can still continue to send static HTML files to the browser, but it can also manipulate the files prior to sending them to the browser. A file that has been manipulated prior to being sent to the browser is referred to as a dynamic web page.

Example

Here’s an example. Let’s say you have a web page on which you wanted the current date to appear. With a static web page, you would have to go in and edit the page every single day to update the date. That would get tiresome pretty quick! The HTML code would look something like this:

With PHP you can let the server make the changes for you. In other words, PHP can

manipulated? That’s PHP the language , which tells the server how and where the page should be manipulated prior to sending it to the browser. In other words, PHP is a programming language that is used to create dynamic web pages.

How does the server know which parts of the page should be static and which parts should be dynamic? In general, the server leaves the page alone. However, if it sees the text **** which signals the server to go back to sending the page as is.

A little history

PHP was originally created by Rasmus Lerdorf in 1995. The main implementation of PHP is now produced by The PHP Group and serves as the formal reference to the PHP language. PHP is free software released under the PHP License, which is incompatible with the GNU General Public License (GPL) due to restrictions on the usage of the term PHP. While PHP originally stood for Personal Home Page, it is now said to stand for PHP: Hypertext Preprocessor, a recursive acronym.

Installing and Configuring PHP

Introduction

Before you can begin using PHP, you need to have a copy of it. For the purposes of this book, we’re going to install PHP on your local computer so you can play around with it without too much fuss. Note that applications you write on your local computer can only be used on your local computer, or by other computers on your local network. For the rest of this book, we are going to need Apache, MySQL, and PHP. Together, this collection is referred to as AMP. When you run this on Linux, it’s called LAMP, and when you run it on Windows, it’s called WAMP. Some people call this combination a “stack”, and may refer to the combination as the “LAMP Stack”. Fortunately, this is a very popular combination and you don’t have to figure it all out on your own.

Windows Users

As a Windows user myself, I can personally vouch for the ease of installation and use and of the WampServer, which can be found at http://www.wampserver.com/en/ “WampServer is a Windows web development environment. It allows you to create web applications with Apache2, PHP and a MySQL database. WampServer also includes a program called PhpMyAdmin which allows you to easily manage your databases.” As part of the installation, the “www” directory will be automatically created (usually at c:\wamp\www) Create a subdirectory in “www” and put your PHP files inside that folder.

Using Wampserver

Once Wampserver is installed, you start it by selecting start WampServer from the Windows Start menu, as shown below:

If it is not on your Start menu, search for programs that start with WAM, as shown

How Do I Know it is Working?

OK, that was easy, but how can you check that everything really works? Just type in the following URL at your favorite web browser: http://localhost Windows users will see something like this:

Mac users will see something like this:

Oracle VM Virtual Box

If you are proficient with computers, you might like to try out a pre-configured virtual machine. Oracle VirtualBox is free for you to use, and folders can be shared between the host and guest machine allowing you to simply save the file you are working on and refresh your browser, there’s no need to upload via FTP/SFTP to test your changes. If this sounds like a good option for you, here’s a link to a tutorial on setting it up. Using Oracle Virtual Box is outside the scope of this book.

Installing for a Web Site

If you wanted to make your application available to everybody on the Internet, you’ll need to install PHP and your application onto a publicly accessible server. This topic is covered in Appendix A: Installing PHP on a Website.

Exercise

Use your editor (for instance, Programmers’ Notepad) to create a file containing the following line:

Save the file as phpinfo.php in the correct place on your hard drive (for instance, C:\wamp\www). Finally, open the file with a browser by typing http://localhost/phpinfo.php

Basic Elements of HTML

All HTML based documents have the same basic elements. They are composed of tags that define the various parts of the document—from where it starts and ends, and everything in between. HTML uses elements (“tags”) to mark up sections of text. These can include headings, subtitles, lists, bold or underlined text and, of course, links. HTML documents read from left to right and top to bottom. Tags To distinguish tags from ordinary text, tags appear inside brackets: < and >. Most tags have an open and close tag, also known as a start and end tag. The open tag starts with < and end tag starts with ** indicates to start bold and **** indicates to stop (end) bold. For example here is a paragraph element:

In this example the

and

are the tags: they are used to delineate the text contained within as a paragraph. Something worth pointing out here is that you don’t have to put everything on a single line. The code above works just as well as this below:

In fact, the indentation isn’t needed either, although it certainly improves the readability. Keep in mind that someone (maybe you) may have to edit your HTML in the future, so making it readable is a good idea. All tag formats are the same. They begin with a less-than sign: < and end with a greater- than sign: >. Always. What goes inside the < and > is the tag name. A big part of learning HTML is learning the specific tags and what they do. Nested Tags In general, most tags can be nested inside other tags, but of course there may be exceptions to this rule. Here you see the bold tag nested inside of a paragraph tag:

Not all elements have both an opening and closing piece. For example,
doesn’t

have a corresponding
, and neither does .

Required tags

An HTML page starts with the tag and ends with . The body of the page goes inside body tags.

DocType If a webpage is missing a tag or has some sort of “transitional” doctype tag, the page will be rendered in what is called ‘quirks’ mode. Quirks mode is somewhat unpredictable, and you don’t always get what you expect. So, it is important to have a doctype tag if you want your webpage to display in Standards mode, as expected. Head The head of the document is where the Title and Meta information will go. Generally, you would also put any CSS styles, script tags, and link tags to external files in the Head also, if you have any.

Optional Tags

Meta Tags