當前位置:首頁 » 編程語言 » phpfloattoint

phpfloattoint

發布時間: 2022-04-28 10:38:22

php 怎麼將字元轉成數字

第一種轉換方式: 強制轉換;

代碼:

(1)phpfloattoint擴展閱讀:

PHP的數據類型轉換屬於強制轉換,允許轉換的PHP數據類型有:

(int)、(integer):轉換成整形;

(float)、(double)、(real):轉換成浮點型;

(string):轉換成字元串;

(bool)、(boolean):轉換成布爾類型;

(array):轉換成數組;

(object):轉換成對象。

⑵ php 字元串轉float

思路:

先把字元串轉成10進制整數,再把整數轉成float數值

具體代碼

$str='0f00391a';

$d=hexdec($str);

var_mp($d);

$f=floatval($d);

var_mp($f);

輸出:

int(251672858)
float(251672858)

⑶ 在php中,怎樣把數字轉化為字元串

1、首先需要新建一個74.php。

⑷ php整數用強制轉換命令(double)轉換為浮點數。

要用printf格式化輸出。
php好像不需要強制轉化類型,會自動轉化的。

⑸ php怎麼查看一個變數的佔用內存

我們在前面的php高效寫法提到,盡量不要復制變數,特別是數組。一般來說,PHP數組的內存利用率只有 1/10, 也就是說,一個在C語言裡面100M 內存的數組,在PHP裡面就要1G。下面我們可以粗略的估算PHP數組佔用內存的大小,首先我們測試1000個元素的整數佔用的內存:

[php] view plain print?
<?php
echo memory_get_usage() , '<br>';
$start = memory_get_usage();
$a = Array();
for ($i=0; $i<1000; $i++) {
$a[$i] = $i + $i;
}
$mid = memory_get_usage();
echo memory_get_usage() , '<br>';
for ($i=1000; $i<2000; $i++) {
$a[$i] = $i + $i;
}
$end = memory_get_usage();
echo memory_get_usage() , '<br>';
echo 'argv:', ($mid - $start)/1000 ,'bytes' , '<br>';
echo 'argv:',($end - $mid)/1000 ,'bytes' , '<br>';

輸出是:

353352
437848
522024
argv:84.416bytes
argv:84.176bytes

大概了解1000
個元素的整數數組需要佔用 82k 內存,平均每個元素佔用 84 個位元組。而純 C 中整體只需要 4k(一個整型佔用4byte * 1000
)。memory_get_usage() 返回的結果並不是全是被數組佔用了,還要包括一些 PHP
運行本身分配的一些結構,可能用內置函數生成的數組更接近真實的空間:

[php] view plain print?
<?php
$start = memory_get_usage();
$a = array_fill(0, 10000, 1);
$mid = memory_get_usage(); //10k elements array;
echo 'argv:', ($mid - $start )/10000,'byte' , '<br>';
$b = array_fill(0, 10000, 1);
$end = memory_get_usage(); //10k elements array;
echo 'argv:', ($end - $mid)/10000 ,'byte' , '<br>';

得到:
argv:54.5792byte
argv:54.5784byte

從這個結果來看似乎一個數組元素大約佔用了54個位元組左右。

首先看一下32位機C語言各種類型佔用的位元組:

[cpp] view plain print?
#include "stdafx.h"
//#include <stdio.h>

int main() {
printf("int:%d\nlong:%d\ndouble:%d\nchar*:%d\nsize_t:%d\n",
sizeof(int), sizeof(long),
sizeof(double), sizeof(char *),
sizeof(size_t));
return 0;
}

int:4
long:4
double:8
har*:4
size_t:4
在PHP中都使用long類型來代表數字,沒有使用int類型
大家都明白PHP是一種弱類型的語言,它不會去區分變數的類型,沒有int float char *之類的概念。
我們看看php在zend裡面存儲的變數,PHP中每個變數都有對應的 zval, Zval結構體定義在Zend/zend.h裡面,其結構:

[cpp] view plain print?
typedef struct _zval_struct zval;
struct _zval_struct {
/* Variable information */
zvalue_value value; /* The value 1 12位元組(32位機是12,64位機需要8+4+4=16) */
zend_uint refcount__gc; /* The number of references to this value (for GC) 4位元組 */
zend_uchar type; /* The active type 1位元組*/
zend_uchar is_ref__gc; /* Whether this value is a reference (&) 1位元組*/
};

PHP使用一種UNION結構來存儲變數的值,即zvalue_value 是一個union,UNION變數所佔用的內存是由最大

成員數據空間決定。

[cpp] view plain print?
typedef union _zvalue_value {
long lval; /* long value */
double dval; /* double value */
struct { /* string value */
char *val;
int len;
} str;
HashTable *ht; /* hash table value */
zend_object_value obj; /*object value */
} zvalue_value;

最大成員數據空間是struct str,指針占*val用4位元組,INT佔用4位元組,共8位元組。

struct zval佔用的空間為8+4+1+1 = 14位元組,

其實呢,在zval中數組,字元串和對象還需要另外的存儲結構,數組則是一個 HashTable:

HashTable結構體定義在Zend/zend_hash.h.

[cpp] view plain print?
typedef struct _hashtable {
uint nTableSize;//4
uint nTableMask;//4
uint nNumOfElements;//4
ulong nNextFreeElement;//4
Bucket *pInternalPointer; /* Used for element traversal 4*/
Bucket *pListHead;//4
Bucket *pListTail;//4
Bucket **arBuckets;//4
dtor_func_t pDestructor;//4
zend_bool persistent;//1
unsigned char nApplyCount;//1
zend_bool bApplyProtection;//1
#if ZEND_DEBUG
int inconsistent;//4
#endif
} HashTable;
HashTable 結構需要 39 個位元組,每個數組元素存儲在 Bucket 結構中:

[cpp] view plain print?
typedef struct bucket {
ulong h; /* Used for numeric indexing 4位元組 */
uint nKeyLength; /* The length of the key (for string keys) 4位元組 */
void *pData; /* 4位元組*/
void *pDataPtr; /* 4位元組*/
struct bucket *pListNext; /* PHP arrays are ordered. This gives the next element in that order4位元組*/
struct bucket *pListLast; /* and this gives the previous element 4位元組 */
struct bucket *pNext; /* The next element in this (doubly) linked list 4位元組*/
struct bucket *pLast; /* The previous element in this (doubly) linked list 4位元組*/
char arKey[1]; /* Must be last element 1位元組*/
} Bucket;

Bucket
結構需要 33 個位元組,鍵長超過四個位元組的部分附加在 Bucket 後面,而元素值很可能是一個 zval 結構,另外每個數組會分配一個由
arBuckets 指向的 Bucket 指針數組, 雖然不能說每增加一個元素就需要一個指針,但是實際情況可能更糟。這么算來一個數組元素就會佔用
54 個位元組,與上面的估算幾乎一樣。

一個空數組至少會佔用 14(zval) + 39(HashTable) + 33(arBuckets) = 86
個位元組,作為一個變數應該在符號表中有個位置,也是一個數組元素,因此一個空數組變數需要 118
個位元組來描述和存儲。從空間的角度來看,小型數組平均代價較大,當然一個腳本中不會充斥數量很大的小型數組,可以以較小的空間代價來獲取編程上的快捷。但如果將數組當作容器來使用就是另一番景象了,實際應用經常會遇到多維數組,而且元素居多。比如10k個元素的一維數組大概消耗540k內存,而10k
x 10 的二維數組理論上只需要 6M 左右的空間,但是按照 memory_get_usage
的結果則兩倍於此,[10k,5,2]的三維數組居然消耗了23M,小型數組果然是劃不來的。

⑹ php 怎麼把數字型轉為字元型

在要轉換的變數之前加上用括弧括起來的目標類型
使用3個具體類型的轉換函數,intval()、floatval()、strval()
使用通用類型轉換函數settype(mixed var,string type)

第一種轉換方式: (int) (bool) (float) (string) (array) (object)

<?php
$num1=3.14;
$num2=(int)$num1;
var_mp($num1); //輸出float(3.14)
var_mp($num2); //輸出int(3)
?>

第二種轉換方式: intval() floatval() strval()

<?php
$str="123.9abc";
$int=intval($str); //轉換後數值:123
$float=floatval($str); //轉換後數值:123.9
$str=strval($float); //轉換後字元串:"123.9"
?>

第三種轉換方式: settype();

<?php
$num4=12.8;
$flg=settype($num4,"int");
var_mp($flg); //輸出bool(true)
var_mp($num4); //輸出int(12)
?>

⑺ PHP怎麼把超過10位長度的數字轉換成整形求助大神!

$i="12345678910";
$i2="12245678910";
echo(float)$i-(float)$i2;//輸出結果正確

$i="12345678910";
$i2="12245678910";
echo(int)$i-(int)$i2;//輸出結果為0,因為int型最大為27...,所以int轉換後兩個值都一樣

⑻ php 圖象處理函數 imagestring 函數的運用

可以用imagettftext來生成,支持truetype字體

array imagettftext ( resource image, float size, float angle, int x, int y, int color, string fontfile, string text )

image
圖像資源。見 imagecreatetruecolor()。

size
字體大小。根據 GD 版本不同,應該以像素大小指定(GD1)或點大小(GD2)。

angle
角度製表示的角度,0 度為從左向右讀的文本。更高數值表示逆時針旋轉。例如 90 度表示從下向上讀的文本。

x
由 x,y 所表示的坐標定義了第一個字元的基本點(大概是字元的左下角)。這和 imagestring() 不同,其 x,y 定義了第一個字元的左上角。例如 "top left" 為 0, 0。

y
Y 坐標。它設定了字體基線的位置,不是字元的最底端。

color
顏色索引。使用負的顏色索引值具有關閉防鋸齒的效果。見 imagecolorallocate()。

fontfile
是想要使用的 TrueType 字體的路徑。

根據 PHP 所使用的 GD 庫的不同,當 fontfile 沒有以 / 開頭時則 .ttf 將被加到文件名之後並且會在庫定義字體路徑中嘗試搜索該文件名。

當使用的 GD 庫版本低於 2.0.18 時,一個空格字元 而不是分號將被用來作為不同字體文件的「路徑分隔符」。不小心使用了此特性將會導致一條警告信息:Warning: Could not find/open font。對受影響的版本來說唯一解決方案就是將字體移動到不包含空格的路徑中去。

很多情況下字體都放在腳本的同一個目錄下。下面的小技巧可以減輕包含的問題。 <?php
// Set the enviroment variable for GD
putenv('GDFONTPATH=' . realpath('.'));

// Name the font to be used (note the lack of the .ttf extension)
$font = 'SomeFont';
?>

text
文本字元串。

可以包含十進制數字化字元表示(形式為:€)來訪問字體中超過位置 127 的字元。UTF-8 編碼的字元串可以直接傳遞。

如果字元串中使用的某個字元不被字體支持,一個空心矩形將替換該字元。

imagettftext() 返回一個含有 8 個單元的數組表示了文本外框的四個角,順序為坐下角,右下角,右上角,左上角。這些點是相對於文本的而和角度無關,因此「左上角」指的是以水平方向看文字時其左上角。

例子 1. imagettftext() 例子

本例中的腳本將生成一個白色的 400x30 像素 PNG 圖像,其中有黑色(帶灰色陰影)Arial 字體寫的「Testing...」。

<?php
// Set the content-type
header("Content-type: image/png");

// Create the image
$im = imagecreatetruecolor(400, 30);

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);

// The text to draw
$text = 'Testing...';
// Replace path by your own font path
$font = 'arial.ttf';

// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);

// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);

// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>

⑼ php 中怎麼把字元串轉為整形啊 跪求!

intval()當然不行,因為已經超過int自身的長度啦,所以肯定會被截短,可以用隱性轉換嘛,因為php本身就是弱類型的,所以搞不懂LZ為什麼要轉,在程序裡面使用本來就會自動幫你轉類型的,如果 非要轉,可以這樣:
$str = "1157425104434495489";
$int_str = $str*1;

對比一下,var_mp($str == $int_str) //輸出 bool true;

⑽ PHP將在需要時自動將字元串轉換為數字

代碼:

(10)phpfloattoint擴展閱讀:

PHP的數據類型轉換屬於強制轉換,允許轉換的PHP數據類型有:

(int)、(integer):轉換成整形;

(float)、(double)、(real):轉換成浮點型;

(string):轉換成字元串;

(bool)、(boolean):轉換成布爾類型;

(array):轉換成數組;

(object):轉換成對象。

熱點內容
超凡先鋒配置不行怎麼辦 發布:2025-05-15 23:27:54 瀏覽:530
win7取消加密 發布:2025-05-15 23:26:37 瀏覽:470
不用internet打開ftp 發布:2025-05-15 23:06:00 瀏覽:153
sql字元串取數字 發布:2025-05-15 22:57:45 瀏覽:124
推薦編程課 發布:2025-05-15 22:34:12 瀏覽:618
表拒絕訪問 發布:2025-05-15 22:29:37 瀏覽:978
電腦怎樣解壓文件 發布:2025-05-15 22:25:32 瀏覽:439
dns伺服器怎麼看 發布:2025-05-15 22:17:27 瀏覽:151
3dm的壓縮包 發布:2025-05-15 22:09:23 瀏覽:662
和存儲字長 發布:2025-05-15 21:54:09 瀏覽:515