WordPress database error: [Table 'wp_comments' is marked as crashed and should be repaired]
SELECT ID, COUNT( comment_ID ) AS ccount
FROM wp_posts
LEFT JOIN wp_comments ON ( comment_post_ID = ID AND comment_approved = '1')
WHERE ID IN (29,25,28,27,26)
GROUP BY ID
Let’s make some sense of the string, it follows the format of variables name and value. So in the string or as it is technically known is called the “query string”.
url/getform.php?website=phptutorialsite.com&toturiallanguage=php
This means that the value of the variable website = phptutorialsite.
You will notice that that i added in an “&”. The “&” means that we have added another variable into the url, so we are “adding other variables to the string.
So that string reads.
website = phptutorialsite.com
toturiallanguage = php
That was just a simple example, but things can get very complex when you have string with spaces and characters. Filled with characters and spaces and symbols. With this said we enter the world of URL ENCODING. This website will give you the codes used in encoding characters into an URL
http://www.w3schools.com/TAGS/ref_urlencode.asp
The beauty of the encoding is that it is all done automatically so when you are developing and using the GET function you don’t need to worry about doing the url encoding.
In your php file, in out case would be “getform.php” you will just use the variables
echo $website and echo tutoriallanguage.
The disadvantage or problem with using the GET method is that the information is passed through the URL and there is a limit to how much information you can pass in the URL.
]]>After integer.. maybe just after integer, strings are possibly the most important variable types in programming. Storing a bunch (series) of character as one variable. PHP give you alot of functions to manipulate and handle strings.
You can think of string as an array of characters, and individuals characters can be accessed using the index of the character withing the string.
< ?php
$my_string = 'My First String in Php';
$our_string = "My Second String in Php";
$h = "Hello";
$w = "World";
$hw = $h . $w; // appending Hello and World
$h .= $w; // Hello World appending world to hello
$first_letter = $hw[0];
$last_letter = $hw[strlen($hw)-1];
?>
In printing the last character in my string the function strlen was used. Strlen, return the lenght of the string as an integer. So we used the length of the string minus 1, since we are using arrays that start at 0, to get the last character of the string.
Next very useful function is chr, which takes the ascii value and translate it into a string. You can use chr to generate very good passwords
< ?php
$password_len = 5;
$password = "";
$inex = 0;
while($index <= $password_len)
{
$password .= chr(rand(33,126));
$index++;
}
echo $password;
?>
You can check the ASCII-table at http://www.asciitable.com
More string manipulation
Getting Rid of White Space or other Characters
string rtrim ( string str [, string charlist] )
string ltrim ( string str [, string charlist] )
string trim ( string str [, string charlist] )
[, string charlist] optional paramater
rtrim — Strip from the end of a string
ltrim — Strip from the beginning of a string
trim — Strip from the beginning and end of a string
These function returns a string with whitespace stripped from the beginning and end of str. Without the second parameter these functions will strip these characters
” ” (ASCII 32 ), an ordinary space.
“\t” (ASCII 9 ), a tab.
“\n” (ASCII 10), a new line (line feed).
“\r” (ASCII 13), a carriage return.
“\0″ (ASCII 0 ), the NUL-byte.
“\x0B” (ASCII 11 ), a vertical tab.
Sub-String, String-Replace, Change Case
Changing Case
strtoupper() // Changes a string to Upper Case
strtolower() // Changes a string to lower case
These to functions speak for themselves (very good function naming)
ucwords — Uppercase the first character of each word in a string
The definition of a word is any string of characters that is immediately after a whitespace (These are: space, form-feed, newline, carriage return, horizontal tab, and vertical tab).
ucfirst — Make a string’s first character uppercase
Sub-String
Substring (Substr), return the string, starting at the position defined by the start and either to the end of the string or the number of characters defined in the third parameter.
string substr ( string string, int start [, int length] )
[,int length] optional paramater
$orig_string = “phptutorialsite.com” ;
$first_part = substr($orig_string,0,3); // php
$last_part = substr($orig_string,3,16) ; //tutorialsite.com
Strstr & Stristr — Find first occurrence of a string
Strstr and Stristr (case-insensitive searches) returns the part of the string from the first occurrence of the string being searched for until the end of the string. If the string is not found the function returns FALSE (boolean).
string strstr (string where_to_look, string what_to_find)
$orig_string = “phptutorialsite.com”;
$php_tutorial = stristr($orig_string, “php”); //tutorialsite.com
$what_to_find = “PHP”;
$where_to_look = “php Tutorial Site “;
if(strstr($where_to_look,$what_to_find))
{
echo “Yes I found Php Tutorial in this string on this php tutorial site.”;
}
else
{
echo “Php not in this string.”;
}
StrPos - Find position of first occurrence of a string
int strpos ( string where_to_find, mixed what_to_find [, int offset] )
This works just like strstr, however this return the position of the search string within main string. If the string is not found strpos returns FALSE (boolean).
Implode and Explode
The last 2 functions we are going to cover in this tutorial are explode () and implode (). These functions are used in converting strings to arrays and arrays to strings.
Explode takes the elements in a string “seperated” by delimiter and places those elements into an array.
e.g It can take a date 07/08/2006 and put it into an array date, where date[0] = 07, date[1]=08 and date[2]=2006, where “/” is the delimiter.
eg.
string siteurl = “www.phptutorialsite.com”;
$site=explode(”.”, $siteurl);
site[0]=> www
site[1]=> phptutorialsite
site[2]=> com
Implode takes the element of an array, and put them into a string, with the “separator” being the first parameter specified in the implode functions.
e.g
sitename[0]=php
sitename[1]=good
sitename[2]=tutorial
sitename[3]=site
sitename[4]=best
$sitefullname= implode (” “, $sitename);
echo $sitefullname; //php good tutorial site best
Syntax :
explode() => array explode ( string separator, string string [, int limit] )
implode() => string implode ( string glue, array pieces )
Closing
These are just a few of the functions, that can be used to manipulate strings in php, for a full list of string function, you can visit. http://www.php.net/manual/en/ref.strings.php .
echo isset($myVar);
empty()
Empty, is logically opposite to isset, and it provided a little more information on the variable.
If the variable is not created empty return the value of 1, if the variable is define and has the value (0, or “” (null/empty string)) empty returned the value of 1 as well. However if the variable is defined empty does not return any values.
echo empty($myVar);
unset()
Unlike isset and empty, which just returns a value, unset acts on the variable. Unset cleared the varaible from memory and unallocated the variable. The variable name and the value of the variable is completed erased when using unset.
Thus great care should be used when using the function unset, and you should be sure that you dont require that variable or its vale anywhere else in the program.
gettype, settype
Gettype and settype, can be used to to do the variable castinig for you in PHP as well as get the current datatype of the variable. Gettype as the name implies get the variable type for the current variable
$myNum = 8;
echo gettype($myNum) ;
This will display the value “integer” as the type of the variable.
settype($myNum, “string”);
echo gettype ($myNum);
This displays the value “string”.
Examples of how to display constants.
echo “The number of letters in the alphabet are ” . NUMBEROFALPHABETLETTERS ;
echo “New years is generally celebrated on ” . NEWYEARSDAY;
echo PHP_OS; Displays the current (OS) operating system that PHP is running.
PHP has a few define constants, sometimes refer to as magic constants.
__LINE__
The current line number of the file.
__FILE__
The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2, __FILE__ always contains an absolute path with symlinks resolved whereas in older versions it contained relative path under some circumstances.
__DIR__
The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(__FILE__). This directory name does not have a trailing slash unless it is the root directory. (Added in PHP 5.3.0.)
__FUNCTION__
The function name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the function name as it was declared (case-sensitive). In PHP 4 its value is always lowercased.
__CLASS__
The class name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the class name as it was declared (case-sensitive). In PHP 4 its value is always lowercased.
__METHOD__
The class method name. (Added in PHP 5.0.0) The method name is returned as it was declared (case-sensitive).
__NAMESPACE__
The name of the current namespace (case-sensitive). This constant is defined in compile-time (Added in PHP 5.3.0).