The strings manipulation is part of the any programmer routine work. Specifically in the case of PHP, a number of functions, language native, can be used to assist in various activities. In this post, we will address some of the main functions in PHP for string manipulation, in order to assist professionals in tasks of everyday life.

ECHO FUNCTION

Despite the name, echo is no longer a function, since its use is not subject to the use of parameters. Its function is to print on the screen a certain value. As shown in the example below:

<?php
echo “Hello World”;

?>

EXPLODE FUNCTION

This function breaks a string from the value reported in the first parameter. The breaking string is reported in the second parameter and the function return is given in the form of an array. As the example:

<?php

$pizza  = “piece1 piece2 piece3 piece4 piece5 piece6”;
$pieces = explode(” “, $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2

?>

LCFIRST FUNCTION

This function is used when you need to leave in small letters the first character of a string. As example:

<?php
$foo = ‘HelloWorld’;
$foo = lcfirst($foo);  //helloWorld

?>

UCFIRST FUNCTION

This function is used when it is necessary to leave uppercase the first character of a string, as in the following example:

<?php
$foo = ‘helloworld’;
$foo = ucfirst($foo);  //HelloWorld

?>

SUBSTR FUNCTION

The substr() function removes a certain number of elements of a string. The function takes as first parameter the string and, following, the value of exclusion. This can be positive or negative, as the example:

<?php
echo substr(‘abcdef’, 1);     // returns bcdef

$rest = substr(“abcdef”, -1);    // returns “f”

?>

STRINGS ON SCRIPTCASE

To program using Scriptcase you must use PHP language, all functions and validations used on PHP will run perfectly on Scriptcase.

Scriptcase itself has it own fuctions and they are called MACROS, we have more the 150 macros to help you out to develop even more faster.

Here is an example of a Scriptcase Macro:

sc_lookup(Dataset, “SQL Command”, “Connection”)

This macro allows the user to execute SQL commands and returns the result to the “dataset” variable. The “dataset” structure is an array (line/column).

The “connection” parameter is optional. Use when the command is executed in a database different from that specified for the application.
Ex. 1:
sc_lookup(dataset, “select customer_id, customer_name, credit_limit from customers” );

To have access to the first line (Dataset), use :
{customer_id} = {dataset[0][0]};
{customer_name} = {dataset[0][1]};
{credit_limit } = {dataset[0][2]};

To have access to the second line (Dataset), use:
{customer_id} = {dataset[1][0]};
{customer_name} = {dataset[1][1]};
{credit_limit} = {dataset[1][2]};

If occurs error in the execution of the SQL command, the variable attributed to the dataset will return as “false” and the error message will be available in the “dataset_erro” variable. It is also important to verify the select returned data, to prevent access to inexistent variables, once the output array only will be created if the select command returns data.
Ex. 2:
sc_lookup(my_data, “select customer_id, customer_name, credit_limit from customers”);
if ({my_data} === false)
{
echo “Access error. Message=”. {my_data_erro} ;
}
elseif (empty({my_data}))
{
echo “Select command didn’t return data”;
}
else
{
{customer_id} = {my_data[0][0]};
{customer_name} = {my_data[0][1]};
{credit_limit} = {my_data[0][2]};
}

Ex. 3: The SQL command also can be composed of application fields (local variables) or of global variables:
sc_lookup(dataset, “select order_value from orders where clienteid = ‘{customer_id} ‘ and salesman_id = [var_glo_salesman]”);

Note: The command must always finished with semicolon “;”.

Note2: For a big result returned in the dataset we recomend the use of the macro sc_selec instead of this one.

Check the full macro list:

Check out more content on our blog!
Learn all about Scriptcase.

By ,

June 10, 2015

a

You might also like…

“We have developed the Connected Citizen Card, which assists public administrators in decision-making and policy creation.”(Assist Soluções em TI)

Check below how Assist Soluções em TI used Scriptcase to modernize and streamline the development...

AI-Driven Software Development: The Role of ChatGPT

In the rapidly evolving world of technology, artificial intelligence (AI) is playing an increasingl...

Low-Code: The Key to Accessible Digital Transformation

Digital transformation is imperative for companies wishing to remain competitive in the current era...

You might also like…

Get new posts, resources, offers and more each week.

We will use the information you provide to update you about our Newsletter and Special Offers. You can unsubscribe any time you want by clinck in a link in the footer of any email you receive from us, or by contacting us at sales@scriptcase.net. Learn more about our Privacy Police.