Recent Notes
Displaying keyword search results 1 - 10
Created by magnum on September 11, 2011 19:46:09
Last update: September 11, 2011 19:46:09
A pair of C functions convert between an Internet address and a string (ASCII):
#include <arpa/inet.h>
/*
* Returns a poin...
However , these functions do not support IPv6. The new pair is:
#include <arpa/inet.h>
/* Convert a Internet ad...
Examples:
#include <sys/socket.h>
#include <netinet/in.h>...
Created by jinx on May 03, 2011 11:43:26
Last update: May 03, 2011 11:43:26
The PHP function strval returns the string value of a variable. It does the same thing as casting a variable to string, or automatic conversion where string is expected.
Conversion rules:
Type String Value
Boolean TRUE '1'
Boolean FALSE '' (empty string)
integer of float string representing the number
Array The string 'Array'
Object Result of __toString() if defined, error otherwise
Resource 'Resource id #n' , where n is a number assigned to the resource.
NULL '' (empty string)
Example:
<?php
set_error_handler(function($errno, $errst...
Results:
bool(true)
---------------------
strval: '1'...
Created by jinx on May 02, 2011 11:55:53
Last update: May 02, 2011 11:57:20
The PHP function intval returns the integer value of a variable. But there are some gotchas:
The string ' 0x10 ' may look like hex, but it's not recognized as hex by intval unless base 0 is entered as the second argument.
With floating point numbers, round-off errors may produce surprising results (actually, intval truncates, it doesn't round off).
The number base argument is effective only when the variable is a string.
Examples:
<?php
echo 19.99 * 100, "\n"; // 1999
echo i...
Created by jinx on April 29, 2011 13:43:13
Last update: April 29, 2011 13:44:34
List of functions testing variable type:
Function Description
is_bool Finds out whether a variable is a boolean
is_double Alias of is_float()
is_float Finds whether the type of a variable is float
is_int Find whether the type of a variable is integer
is_integer Alias of is_int()
is_long Alias of is_int()
is_null Finds whether a variable is NULL
is_numeric Finds whether a variable is a number or a numeric string
is_object Finds whether a variable is an object
is_real Alias of is_float()
is_scalar Finds whether a variable is a scalar
is_string Find whether the type of a variable is string
Test code:
<?php
class A {
}
$a = ar...
Output:
var_dump: int(1)
-------------------------
i...
Created by jinx on April 16, 2011 14:56:41
Last update: April 16, 2011 14:56:57
The PHP function preg_split splits a string by a regular expression.
Example:
<?php
// split by comma
$colors = "red, gr...
Prints:
array(3) {
[0]=>
string(3) "red"
[1...
Created by jinx on April 10, 2011 20:56:06
Last update: April 10, 2011 20:59:33
The PHP manual says that the count function also counts the properties for an object. Not true. Using count on an object reference always returns 1. In order to count properties, you have to cast the object to an array, which is equivalent to calling get_object_vars on the object. When you cast an object to an array, count is still counting the number of elements in an array!
<?php
class Vegetable {
var $name;
...
Outputs:
array(3) {
["name"]=>
string(7) "Cabbage...
Created by jinx on April 10, 2011 14:43:19
Last update: April 10, 2011 14:43:35
The function substr accepts negative index number, which counts from the end of the string backwards.
<?php
// get the last character
echo substr(...
Created by Fang on August 23, 2010 22:55:58
Last update: August 24, 2010 15:45:04
The tags XML flow control tags are exactly the same as their Core flow control equivalents, except that the test condition with a boolean EL expression is replaced by the select condition with an XPath expression. In the case of the forEach tag, the items attribute is replaced with the select attribute. In a test condition, the XPath expression is evaluated to a boolean value by the rules of the XPath boolean() function, which converts its argument to a boolean as follows: a number is true if and only if it is neither positive or negative zero nor NaN. a node-set is true if and only if it is non-empty. a string is true if and only if its length is non-zero. an object of...
Created by Fang on August 18, 2010 20:07:46
Last update: August 18, 2010 20:11:36
JSTL uses XPath expressions as a concise notation to specify or select parts of an XML document. JSTL provides EL like expressions to access web application data and comes with the core function library of the XPath specification. Accessing Web Application Data XPath Expression Mapping $foo pageContext.findAttribute("foo") $param:foo request.getParameter("foo") $header:foo request.getHeader("foo") $cookie:foo maps to the cookie's value for name foo $initParam:foo application.getInitParameter("foo") $pageScope:foo pageContext.getAttribute("foo", PageContext.PAGE_SCOPE) $requestScope:foo pageContext.getAttribute("foo", PageContext.REQUEST_SCOPE) $sessionScope:foo pageContext.getAttribute("foo", PageContext.SESSION_SCOPE) $applicationScope:foo pageContext.getAttribute("foo", PageContext.APPLICATION_SCOPE) For example, to find the bar element whose x attribute equals the value of the HTTP request parameter named paramName :
/foo/bar[@x=$param:paramName] Java Type to XPath Type Mappings XPath Type Java Type java.lang.Boolean boolean java.lang.Number number java.lang.String string Object exported by <x:parse> node-set Please note that JSTL, as of version 1.2,...
Created by Fang on August 17, 2010 19:27:32
Last update: August 17, 2010 19:27:32
Get the length of a collection The length function returns the size of a collection. It can be applied to any object supported by the JSTL core tag <c:forEach> , namely: Arrays Implementation of java.util.Collection Implementation of java.util.Iterator Implementation of java.util.Enumeration Implementation of java.util.Map String When used on String , it returns the number of characters in the string. Test it Make these additions to the expanded test application : Create a new Java class LengthFunction :
package jstl.demo.handler; import java.... Create a new JSP ( lengthfunction.jsp ) under webapp : <%@ taglib uri="http://java.sun.com/jsp/jstl/c... Compile and package the WAR with: mvn package Deploy the WAR to a servlet container of your choice (for example, Tomcat or JBoss). Test the page with this URL...