Web Design

PHP – Variables and Data Types

PHP – Variables and Data Types

Variables are containers for storing data

PHP has no command for declaring a variable.
A variable is created the moment you first assign a value to it.

Rules for PHP variables:

A variable starts with the $ sign, followed by the name of the variable
A variable name must start with a letter or the underscore character
A variable name cannot start with a number
A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
Variable names are case sensitive ($y and $Y are two different variables)

Example:


<?php

$txt="Hello world!";
$x=5;
$y=10.5;

?>

Local and Global

A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function.

A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function.

global – Keyword

The global keyword is used to access a global variable from within a function.


<?php
$x=5;
$y=10;

function myTest()
{
global $x,$y; // It takes the global variables x and y 
$y=$x+$y;
}

myTest();
echo $y; // Outputs 15
?>

static – Keyword

Normally, when a function is completed/executed, all of its variables are deleted. However, sometimes we want a local variable NOT to be deleted. We need it for a further job.

To do this, use the static keyword when you first declare the variable:


<?php

function myTest()
{
static $x=0; // the variable is static and it will not be deleted
echo $x;
$x++;
}

myTest(); // output is 0
echo "<br>";
myTest(); // output is 1
echo "<br>";
myTest(); // output is 2
echo "<br>";
myTest(); // output is 3
echo "<br>";
myTest(); // output is 4
?>

The result is:

0
1
2
3
4

Data Types

The types of data the variables can content.


<?php 

// STRINGS
// -------------------
$x = "Hello world!";
echo $x;
echo "<br>"; 
$x = 'Hello world!';
echo $x;

// INTEGERS
// -------------------
$x = 5985;
var_dump($x); // var_dump() function returns the data type and value of variables
echo "<br>"; 
$x = -345;    // negative number 
var_dump($x);
echo "<br>"; 
$x = 0x8C;    // hexadecimal number
var_dump($x);
echo "<br>";
$x = 047;     // octal number
var_dump($x);

// FLOATING NUMBERS
// -------------------
$x = 10.365;
var_dump($x); // var_dump() function returns the data type and value of variables
echo "<br>"; 
$x = 2.4e3;
var_dump($x);
echo "<br>"; 
$x = 8E-5;
var_dump($x);

// BOOLEANS
// -------------------
$x=true;
$y=false;

// NULL
// -------------------
$x="Hello world!";
$x=null;
var_dump($x); 

?>

By |PHP|Commenti disabilitati su PHP – Variables and Data Types

PHP – Basic Structure

PHP – Basic Structure

PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML.

PHP code are executed on the server, and the result is returned to the browser as plain HTML.

In PHP, all user-defined functions, classes, and keywords (e.g. if, else, while, echo, etc.) are case-insensitive.

However; in PHP, all variables are case-sensitive.

PHP files have extension “.php”

The official PHP website is: http://www.php.net

<!DOCTYPE html>
<html>
<body>

<?php

// This is a one-line c++ style comment

# This is a one-line shell-style comment

/* This is a multi line comment
yet another line of comment */

echo "Hello World!";
?>

</body>
</html>

Notice:
The PHP code is tagged inside:

<?php
... your php code ... ;
?>

or with the short notation
<?
... your php code ... ;
?>

By |PHP|Commenti disabilitati su PHP – Basic Structure

WordPress 3 – Database Structure

Wordpress 3 – Database Structure

wp-database-structure

By |WordPress|Commenti disabilitati su WordPress 3 – Database Structure

WordPress – Aggiungere un Search Box

Wordpress – Aggiungere un Search Box

Aggiungiamo automaticamente un Search Box all’interno di ogni articolo pubblicato:

1. Apriamo con un software FTP nel server la cartella che contiene il nostro tema WoprdPress, ad esempio:
blog/wp-content/themes/miotema

2. Apriamo il file:
blog/wp-content/themes/miotema/single.php che contiene il template per il singolo articolo e aggiungiamo le righe:

<! -- Search Form START -->
<form method="get" id="searchform" action="<?php bloginfo('home'); ?>/">
<div><input type="text" size="18" value="<?php echo wp_specialchars($s, 1); ?>" name="s" id="s" />
<input type="submit" id="searchsubmit" value="Search" class="btn" />
</div>
</form>
<! -- Search Form END -->

3. Uppiamo il file modificato sovrascrivendo quello vecchio

4. Dal browser apriamo un articolo qualunque per verificare la presenza del nuovo campo di ricerca.

By |WordPress|Commenti disabilitati su WordPress – Aggiungere un Search Box

WordPress – Posts – Lista dei post recenti

Wordpress – Posts – Lista dei post recenti

Lista dei post recenti | solo titolo

Aggiungiamo automaticamente una lista con i post recenti:

1. Apriamo con un software FTP nel server la cartella che contiene il nostro tema WoprdPress, ad esempio:
blog/wp-content/themes/miotema

2. Apriamo il file:
blog/wp-content/themes/miotema/single.php che contiene il template per il singolo articolo e aggiungiamo le righe:

<! -- Get Recent Posts START -->
<?php wp_get_archives( array( 'type' => 'postbypost', 'limit' => 10, 'format' => 'html' ) ); ?>
<! -- Get Recent Posts END -->

limit’ => 10 : il numero massimo di post da visualizzare

Il risultato sarà un elenco puntato dal post più recente a quello più vecchio.

Lista dei post recenti | titolo + sommario

<! -- Get Recent Posts START -->
<ul>
<?php $the_query = new WP_Query( 'showposts=5' ); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<li><?php the_excerpt(__('(more…)')); ?></li>
<?php endwhile;?>
</ul>
<! -- Get Recent Posts END -->

showposts=5 : numero di post mostrati

By |WordPress|Commenti disabilitati su WordPress – Posts – Lista dei post recenti