i have this code
that count one string accorances i just cant explain my use but it really nice for me plese see here https://ideone.com/pvAIjW
code:
<?php
$te = 'abcdefghijklmnopqrstuvwxyz1234567891011121314151617181920';
$text=strlen($te);
if($text <=10)
{
$g == 2;
}
else
{
$x=$text;
$y=2;
$tempMod = (float)($x / $y);
$tempMod = ($tempMod - (int)$tempMod)*$y;
if($tempMod ==0)
{
$g = $x / $y;
}
else
{
$x = $x+1;
$g = $x / $y;
}
}
echo $g;
in my achievement i want it as a function so i just call
$output=function($string);
echo $output to get my result
big thanks in advance
That would be like this. Just change all the instance of $te (although you can still use $te if you want, I just changed it to avoid confusion) with the parameter name (which is $string), then instead of echoing $g, return it:
function functionName($string){
$text=strlen($string);
if($text <=10)
{
$g == 2;
}
else
{
$x=$text;
$y=2;
$tempMod = (float)($x / $y);
$tempMod = ($tempMod - (int)$tempMod)*$y;
if($tempMod ==0)
{
$g = $x / $y;
}
else
{
$x = $x+1;
$g = $x / $y;
}
}
return $g;
}
$te = 'abcdefghijklmnopqrstuvwxyz1234567891011121314151617181920';
$varName = functionName($te);
NOTE: Not to be rude, but this kind of problem can be easily searched, so I suggest you try reading documentations pertaining to PHP or find online tutorials, most of the time they cover using and creating functions
<?php
$te = 'abcdefghijklmnopqrstuvwxyz1234567891011121314151617181920';
function name($value){
$text=strlen($value);
if($text <=10){
$value == 2;
}
else{
$x=$text;
$y=2;
$tempMod = (float)($x / $y);
$tempMod = ($tempMod - (int)$tempMod)*$y;
if($tempMod ==0){
$g = $x / $y;
}else{
$x = $x+1;
$value = $x / $y;
}
}
return $value;
}
echo name($te);
function functionName($string){
if(strlen($string) <=10) $g == 2;
else {
$x=strlen($string);
$y=2;
if(!(((float)($x / $y) - (int)((float)($x / $y)))*$y ==0)) $x++;
$g = $x / $y;
}
return $g;
}
$te = 'abcdefghijklmnopqrstuvwxyz1234567891011121314151617181920';
$varName = functionName($te);
var_dump($varName);
The code perhaps will be better shorter. Up to you.
Only you have to do is to send the $te variable like a parameter to the function:
function fnct($te){
$text=strlen($te);
if($text <=10)
{
$g == 2;
}else{
$x=$text;
$y=2;
$tempMod = (float)($x / $y);
$tempMod = ($tempMod - (int)$tempMod)*$y;
if($tempMod ==0)
{
$g = $x / $y;
}else{
$x = $x+1;
$g = $x / $y;
}
}
return $g;
}
And when you want to call this function you will use this code:
$te = 'abcdefghijklmnopqrstuvwxyz1234567891011121314151617181920';
$output = fnct($te);
echo $output;
Related
function test_loop($x_values,$x, $y)
{
$x = $x + 1;
if($x < 10)
{
$x_values[] = $x."#"; // insert item 2#,3#,4# into array
}
//loop again if y is not = 3;
$y = $y + 1;
if($y < 5)
{
test_loop($x_values, $x, $y);
}
return $x_values;
}
function abc(){
$bababa = test_loop([],1,0);
foreach($bababa as $fpackage_id)
{
echo $fpackage_id;
}
}
abc();
Output :
2#
How to make the output become :
2#,3#,4#
if($y < 5)
{
test_loop($x_values, $x, $y);
}
You're not doing anything with the return value of the recursive function call.
You need to add the array returned from test_loop() to the existing array:
$x_values += test_loop($x_values, $x, $y);
Your code now prints: 2#3#4#5#6#
If you want the output to be 2#, 3#, 4#, 5#, 6#, you can use the implode() function instead of a loop:
echo implode (', ', $bababa);
You'll want to utilize pass by reference:
https://www.php.net/manual/en/language.references.pass.php
I haven't looked too closely at your code, you may need to make more changes than this... but this might work:
function test_loop(&$x_values,$x, $y)
{
$x = $x + 1;
if($x < 10)
{
$x_values[] = $x."#"; // insert item 2#,3#,4# into array
}
//loop again if y is not = 3;
$y = $y + 1;
if($y < 5)
{
test_loop($x_values, $x, $y);
}
return $x_values;
}
function abc(){
$bababa = test_loop([],1,0);
foreach($bababa as $fpackage_id)
{
echo $fpackage_id;
}
}
abc();
You should use array_push() function to add an element in the array
Documentation
if($x < 10)
{
array_push($x_values,$x.'#');
}
function test_loop($x, $y)
{
static $x_values = array();
$x = $x + 1;
if($x < 10)
{
$x_values[] = $x."#"; // insert item 2#,3#,4# into array
}
//loop again if y is not = 3;
$y = $y + 1;
if($y < 5)
{
test_loop($x, $y);
}
echo "gula</br>";
return $x_values;
}
function abc(){
$bababa = test_loop(1,0);
foreach($bababa as $fpackage_id)
{
echo $fpackage_id;
}
}
abc();
Output :
gula
gula
gula
gula
gula
2#3#4#5#6#
The code call test_loop() function before echo "gula", so supposedly it should fail to echo "gula". How to make the Output become like below :
gula
2#3#4#5#6#
UPDATE :
I tried to move the echo and return into else{} statement as #Joel Hager suggested, end up no output on the return value.
function test_loop($x, $y)
{
static $x_values = array();
$x = $x + 1;
if($x < 10)
{
$x_values[] = $x."#"; // insert item 2#,3#,4# into array
}
//loop again if y is not = 3;
$y = $y + 1;
if($y < 5)
{
test_loop($x, $y);
}else{
echo "gula</br>";
return $x_values;
}
}
You should add a return statement on you recursive call to test_loop
if($y < 5)
{
return test_loop($x, $y);
}
This way the code execution goes back to test_loop.
This my coding I'm not getting my answer via Infunction Value '$z'. Please Help
<form action="functions.php" method="post">
<input type="text" name="math">
</form>
<?php
$x = $_POST['math'];
function table($x, $y) {
$z = $x * $y;
return $z;
}
for ($y = 1; $y <=10; $y++) {
echo $x ."*". $y ."=". "<br>";
}
You have to call your function to get the output like below:-
for ($y = 1; $y <=10; $y++) {
$output = table($x, $y);
echo $x ."*". $y ."=". $output . "<br>";
}
A sample output:- https://eval.in/872649
You need to wrap php code inside isset() like below:-
<?php
if(isset($_POST['math'])){
$x = $_POST['math'];
function table($x, $y) {
$z = $x * $y;
return $z;
}
for ($y = 1; $y <=10; $y++) {
$output = table($x, $y);
echo $x ."*". $y ."=". $output . "<br>";
}
}
You need to change your loop as below.. You don't have call the function table anywhere in your code. call it in loop
for ($y = 1; $y <=10; $y++) {
$z = table($x, $y);
echo $x ."*". $y ."=".$z ."<br>";
}
I wanted to have a result like this
50+2+2+2+2 = 58
But I'm getting these kind of result
50
2
2
2
2
these are my codes.
<?php
$height = 50;
echo $height;
function hey($x)
{
return $height += $x;
}
$i = 4;
while($i != 0)
{
echo "<br>".hey(2);
$i--;
}
?>
Please take note that the location of my variable and loop must be and really meant at that position.
what do i need to change on my code. I am new in using php functions.
thanks for the help.
You can use a global variable like this try:
function hey($x)
{
global $height;
return $height += $x;
}
And print the variable height only after the called function.
If you don't put global before the variable inside the function it the seems that you create a new variable inside your function. With global you tell to the server to take the variable that you have created outside the function
In this function:
function hey($x)
{
return $height += $x;
}
$height is not in scope, so it is undefined. You should pass it in:
function hey($x, $height)
{
return $height += $x;
}
Then call it like this:
hey(2, $height);
this is scope problem :
function hey($x)
{
global $height;
return $height += $x;
}
Change to:
global $height;
then
while($i != 0)
{
echo "+".hey(2);
$i--;
}
echo "=" . $height;
I don't understand that you want, but if you need output like that, please try this code..
br mean go to bottom, so I delete it.
<?php
$height = 50;
echo $height;
function hey($x)
{
echo " + $x";
return $x;
}
$i = 4;
while($i != 0)
{
$height += hey(2);
$i--;
}
echo " = $height";
?>
This is online demo: http://phpfiddle.org/main/code/6h1-x5z
<?php
function getResult($height = 50, $increment = 2, $times = 4){
echo $height."+";
$total = 0;
for ($i = 0; $i < $times; $i++){
$total += $increment;
if ($i != ($times-1)){
echo $increment."+";
}
else{
echo $increment." = ".($height+$total);
}
}
}
//usage
getResult(50,2,4);
//The print out: 50+2+2+2+2 = 58
?>
I tried and tried and tried to get this code to work and kept coming up with zilch. So I decided to try it using "for loops" instead and it worked first try. Could somebody tell me why this code is no good?
<?php
$x = $y = 10;
while ($x < 100) {
while ($y < 100) {
$num = $x * $y;
$numstr = strval($num);
if ($numstr == strrev($numstr)) {
$pals[] = $numstr;
}
$y++;
}
$x++;
}
?>
you should reset y=10 inside the first while.
$x = 10;
while ($x < 100) {
$y = 10;
while ($y < 100) {
$num = $x * $y;
$numstr = strval($num);
if ($numstr == strrev($numstr)) {
$pals[] = $numstr;
}
$y++;
}
$x++;
}
You need to reset y before the y loop begins.
While($x < 100){
$y=10; //... rest of code
For loops which loop over an integer that is incremented I would prefer the for-loop:
for ($x=0; $x < 100; $x++) {
for ($y=10; $y<100; $y++) {
$num = $x * $y;
$numstr = strval($num);
if ($numstr == strrev($numstr)) {
$pals[] = $numstr;
}
}
}
IMHO this is much more readable and it's shorter, too.